0

I'm trying to create a photo collage in php by combining two images, a background and an image.

Certain part of my background is transparent, I want to put the image into that part of the background and merge them. The transparent part could be a square, circle, oval rectangle or any other shape. I tried the following code, but it doesn't work

$image_1 = imagecreatefrompng('images/background.png');
$image_2 = imagecreatefrompng('images/image.png');
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagecopy($image_1, $image_2, 0, 0, 0, 0, 100, 100);
imagepng($image_1, 'images/3.png');

The code above combines the two images, but with one of them being very small, and the other take up the whole screen which is not what I wanted. I need to have the the image fit into the transparent part of the background of the newly combined image.

Julia
  • 1,207
  • 4
  • 29
  • 47
  • Possible duplicate of [Merging two images with PHP](https://stackoverflow.com/questions/3876299/merging-two-images-with-php) – Dave Aug 20 '18 at 16:57
  • They are a little different as I need to fit the image to my background whereas the other question is about merging two images. – Julia Aug 20 '18 at 19:13
  • The principals about gathering sizes and adjusting as required should be very close though. – Dave Aug 20 '18 at 19:19
  • The problem I have is not about adjusting sizes. There is a portion of my background that is transparent, it could be an oval, circle, square or anything, and I need the image to fit into that portion. So simply adjusting the size won't resolve my problem because I need to fill the whole transparent part of background. – Julia Aug 20 '18 at 19:22
  • Ah, different problem then. That bit of information should probably be added to your question :) If you don't know where that transparent portion is or, if you do but don't know its shape, I'm not sure how you will be able to fit an image into the transparent area. – Dave Aug 20 '18 at 19:24
  • thanks, I will add that to the original question. – Julia Aug 20 '18 at 19:27
  • I'm not sure you're going to be able to detect the transparent portion of the image or its size using PHP. – Dave Aug 20 '18 at 19:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178377/discussion-between-dave-and-julia). – Dave Aug 20 '18 at 19:50

0 Answers0