title: PHP GD Complex stacking multiple layers
body: <p>I have an image stacking process that I am trying to accomplish with PHP GD. What I have is the following:</p>
  
  <p>3 Images:</p>
  
  <ol>
  <li>Masked clipart</li>
  <li>Clipart texture</li>
  <li>Final background</li>
  </ol>
  
  <p>My masked clipart has black in place of where transparency will be after the texture is applied to the clipart and is transparent for layering over a texture.</p>
  
  <p>The following code sorta works for this:</p>
  
  <pre><code>$im = imagecreatetruecolor($width,$height);
  imagecopy($im, $texture, 0, 0, 0, 0, $width, $height);
  imagecopy($im, $clipart, 0, 0, 0, 0, $width, $height);
  imagecolortransparent($im, imagecolorclosest($clipart, 0, 0, 0));
  </code></pre>
  
  <p>Output is an image with transparent background and the clipart with the texture applied.</p>
  
  <p>However, when I open that file in photoshop, the transparent areas are black which also brings me to the rest of this function:</p>
  
  <p>Now that I have this image, I need to layer it on top of the "Final background" image which will make all the transparency of the previous output now be this "Final background" texture. My thoughts were something like:</p>
  
  <pre><code>$im = imagecreatetruecolor($width,$height);
  imagecopy($im, $texture, 0, 0, 0, 0, $width, $height);
  imagecopy($im, $clipart, 0, 0, 0, 0, $width, $height);    
  imagecolortransparent($im, imagecolorclosest($clipart, 0, 0, 0));
  
  $img = imagecreatetruecolor($width,$height);
  imagecopy($img, $background, 0, 0, 0, 0, $width, $height);
  imagecopy($img, $im, 0, 0, 0, 0, $width, $height);
  imagedestroy($im);
  
  return $img;
  </code></pre>
  
  <p>The problem with this is that is outputs the image with a black background instead of my final texture layer. I believe this code for the final layering may actually work fine and that the black background from the final output and in photoshop is from missing some alpha line in the first part. I have tried to play around with:</p>
  
  <pre><code>imagealphablending( $im, false );
  imagesavealpha( $im, true );
  </code></pre>
  
  <p>Mixing and matching the true / false and alternating only using one of them didn't seem to matter.</p>
  
  <p>If anyone could shed some light on my mistakes here, it would be greatly appreciated.</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The images: <a href="http://imgur.com/a/7SN1S" rel="nofollow">http://imgur.com/a/7SN1S</a></p>
  
  <p>The code:
      
  
  <pre><code>// Layer clipart over texture and convert black to transparent (works)
  $im = imagecreatetruecolor($width,$height);
  imagecopy($im, $texture, 0, 0, 0, 0, $width, $height);
  imagecopy($im, $clipart, 0, 0, 0, 0, $width, $height);    
  imagecolortransparent($im, imagecolorclosest($clipart, 0, 0, 0));
  
  // Layer above image with transparency over background (non-working)
  $img = imagecreatetruecolor($width,$height);
  imagecopy($img, $background, 0, 0, 0, 0, $width, $height);
  imagecopy($img, $im, 0, 0, 0, 0, $width, $height);
  
  header('Content-Type: image/png');
  
  //imagepng($im); // Correctly outputs first step
  imagejpeg($img); // Incorrectly outputs final result
  imagedestroy($im);
  imagedestroy($img);
  </code></pre>