How to make a color transparent in gd lib after merging images?

125 Views Asked by At

I am using gd lib to merge several images, but I'm having problems with the transparency. First I merge several images which each have a transparent background (works fine). Then I want to put another picture on top which consists of three parts: one part that I want to keep, a transparent part where the images below should show and one part in a color (e.g. green #00ff00) which is supposed to become transparent as well after merging. The images are pixel images, so I only want #0f0 to become transparent, none other color.

Here is an image of the result of the code. The first picture shows the merged images ("circle" & "cloud"). The second picture shows the "hat" which is put on top of the merged images. The third picture shows the result I want to achieve. The fourth picture is what I actually get.

Whatever I do, I can't seem to find the solution. I'd be grateful for your help!

    header("Content-type: image/png");
    $img = imagecreatetruecolor(200, 200);
    
    //make an image with transparent background
    $transparency = imagecolorallocatealpha($img, 0, 0, 0, 127);
    imagefill($img, 0, 0, $transparency);
    imagesavealpha($img, true);

    $circle = imagecreatefrompng("testcircle.png"); //black circle
    $cloud = imagecreatefrompng("testcloud.png"); //brown cloud
    $hat = imagecreatefrompng("testhat.png"); //blue hat with green area

    imagecopy($img, $circle, 0, 0, 0, 0, 200, 200);
    imagealphablending($img,true);
    imagecopy($img, $cloud, 0, 0, 0, 0, 200, 200);
    imagealphablending($img,true);

    $green = imagecolorallocate($hat, 0, 255, 0);
    imagecolortransparent($hat, $green);
    imagecopy($img, $hat, 0, 0, 0, 0, 200, 200);
    imagealphablending($img,true);

    /* //With this the green area just keeps being green as in the second picture
    $green = imagecolorallocate($img, 0, 255, 0);
    imagecolortransparent($img, $green);
    imagecopy($img, $hat, 0, 0, 0, 0, 200, 200);
    imagealphablending($img,true);
    */

    imagepng($img);
    imagedestroy($img);
1

There are 1 best solutions below

0
Riyuri On

After searching for a long time, I've found the solution for my problem. I hope it will help if someone has the same question as me in future.

$green = imagecolorallocate($img, 0, 255, 0);
imagecopy($img, $hat, 0, 0, 0, 0, 200, 200);
imagecolortransparent($img, $green);
imagefill($img,0,0,imagecolorallocatealpha($img, 0, 0, 0, 127)); //this line does the trick

First of all I define green as a color. Then I copy the "hat" onto the picture and make the color transparent. Then comes the trick about filling the picture and that makes the green area transparent again. (I found the solution in a german forum).

I noticed that this code only makes the first green area transparent though. If there are two seperated areas (or even more), (at least) one will stay green. So while this is a solution that works for me, there might be one out there that is even better.