3

I have the following scenario. There are two .png images with the exact same size. One is the background image and the other one is the overlay image.

The overlay consists of a transparent background and white letters.

I am trying to combine those two images into one so that I will end up having white text on the background image.

I tried it with this code:

$image_1 = imagecreatefrompng('newimages/'.$overla);

$imgFinal = imagecreatefrompng('imagebackrgounds/background.png');

imagealphablending($imgFinal, true);
imagesavealpha($imgFinal, false);

imagecopy($imgFinal, $image_1, 0, 0, 0, 0, 600, 579);

imagepng($imgFinal, 'new/new.png');

The code above only saves a white picture. What am I doing wrong here? Does someone have a working code?

This is the background picture: enter image description here

This is the image with transparent background ( it should be on top of the background ) enter image description here

miken32
  • 42,008
  • 16
  • 111
  • 154
Dennis
  • 595
  • 1
  • 6
  • 22
  • Possible duplicate of [Merging two images with PHP](http://stackoverflow.com/questions/3876299/merging-two-images-with-php) – Masivuye Cokile Apr 03 '17 at 17:17
  • No it is not because the link you posted only shows how to lay two images on top of each other. But neither one of them are transparent. – Dennis Apr 03 '17 at 17:42
  • according to the duplicate answer, what you are missing is `imagecopymerge()` – Ibu Apr 03 '17 at 17:51
  • I tried that too and it still doesn't work. – Dennis Apr 03 '17 at 18:08
  • @Dennis do you mind posting the images you have wanna try this out – Masivuye Cokile Apr 04 '17 at 09:38
  • @Masivuye Cokile, i posted the pictures. After googling some more yesterday i found out, that php imagecopy doesn't work with png-24 files. You would have to save the transparent files for web in photoshop in order to make this work. I am trying to find a way to do this with png-24 files as well. But so far i didn't. – Dennis Apr 04 '17 at 22:02
  • Does this answer your question? [Combine 2 png-24 transparent Images using Php](https://stackoverflow.com/questions/4514488/combine-2-png-24-transparent-images-using-php) – Martin Jun 12 '22 at 19:33

1 Answers1

0

Solution:

imagecolortransparent($image_1,imagecolorat($image_1,0,0));

Full Code:

$image_1 = imagecreatefrompng('newimages/'.$overla);
imagecolortransparent($image_1,imagecolorat($image_1,0,0));

$imgFinal = imagecreatefrompng('imagebackrgounds/background.png');

imagealphablending($imgFinal, true);
imagesavealpha($imgFinal, false);

imagecopy($imgFinal, $image_1, 0, 0, 0, 0, 600, 579);

imagepng($imgFinal, 'new/new.png');
andrewJames
  • 19,570
  • 8
  • 19
  • 51