title: How to make a color transparent in gd lib after merging images?
body: <p>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 <em>after</em> merging. The images are pixel images, so I only want #0f0 to become transparent, none other color.</p>
  <p><a href="https://i.stack.imgur.com/8ajHb.png" rel="nofollow noreferrer">Here is an image of the result of the code.</a>
  The first picture shows the merged images (&quot;circle&quot; &amp; &quot;cloud&quot;).
  The second picture shows the &quot;hat&quot; 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.</p>
  <p>Whatever I do, I can't seem to find the solution. I'd be grateful for your help!</p>
  <pre><code>    header(&quot;Content-type: image/png&quot;);
      $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(&quot;testcircle.png&quot;); //black circle
      $cloud = imagecreatefrompng(&quot;testcloud.png&quot;); //brown cloud
      $hat = imagecreatefrompng(&quot;testhat.png&quot;); //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);
  </code></pre>