0

This is blowing my mind. I want to merge 2 transparent PNGs.

One is a circle ($source) generated on the fly from a square image. The second is a map marker ($marker) with a transparent circle to fit the first behind.

imagepng($source); and imagepng($marker); outputs just as one might expect. With transparent backgrounds.

I then create an empty transparent image the size of the marker to place them both together:

//NEW BLANK TRANSPARENT IMAGE
$dest = imagecreatetruecolor(50, 61);
$transparent = imagecolorallocatealpha($dest, 0, 0, 0, 127);
imagefill($dest, 0, 0, $transparent);
imagealphablending($dest, true);
imagesavealpha($dest,true);

//COPY THE CIRCLE       
imagecopy($dest, $source, 5, 5, 0, 0, 41, 41);

//AND THE MARKER ON TOP
imagecopy($dest, $marker, 0, 0, 0, 0, 50, 61);

In the result, the circle appears as a black square with the circle inside. I tried a lot of combinations of imagealphablending and imagesavealpha both on $source and $dest and nothing seems to work.

How can I remove the black square and leave the $source transparent in the result as it is before the merge?

Bruno
  • 31
  • 4
  • I just tried your code with 2 transparent images and it worked for me. Tried with `imagecolortransparent($dest, $blackColor);`? – ulentini Mar 29 '13 at 18:23
  • Just did it. Even though it could clash with other blacks in the picture. Still didn't work. – Bruno Mar 29 '13 at 19:22
  • The problem here is in the circle mask. It works fine on it's own, but when copied onto another image it adds a black background to it. – Bruno Mar 29 '13 at 19:23

1 Answers1

0

The problem here was:

To generate the circle on the fly, I was creating a circular mask and making everything else transparent making it red and then making red transparent. This is incompatible with imagesavealpha(true) as it says the gd engine exactly to ignore transparent colors and save the whole alpha channel instead.

When merging both images, the color once transparent is now black.

The solution to my case was to analise it pixel per pixel and copy it to a blank image if it's inside the circle using the circular equation.

Bruno
  • 31
  • 4