title: PHP GD - How do I create a new png image from 3 png images in layers in order
body: <p>I have the following script, It's very simple, get 3 png images, lay the background down, put the icon ontop of that, then add a watermark to top of all of that.</p>
  
  <p>Currently my script produces a weird coloured image once i've created it.<br>
  (Show here: <a href="http://i.stack.imgur.com/yZJ6S.png" rel="nofollow">http://i.stack.imgur.com/yZJ6S.png</a>)</p>
  
  <p>Script: (Run with <code>php -S localhost:9001</code> or <code>php gd.php</code> from CLI)</p>
  
  <pre><code>&lt;?php
  // Download the image files if we don't have them
  function get_file($file, $from) {
      if (!file_exists(__DIR__ . "/" . $file)) { file_put_contents(__DIR__ . "/" . $file, file_get_contents($from)); }
  }
  get_file("background-layer-1.png", "http://i.imgur.com/6pgf3WK.png");
  get_file("icon-layer-2.png", "http://i.imgur.com/0sJt52z.png");
  get_file("stars-layer-3.png", "http://i.imgur.com/1Tvlokk.png");
  get_file("expected.png", "http://i.imgur.com/f7UWKA8.png"); // I want it looking like this
  get_file("actual.png", "http://i.imgur.com/lQJoFlg.png"); // It's actually like this
  
  $bgFile = __DIR__ . "/background-layer-1.png"; // 93 x 93
  $imageFile = __DIR__ . "/icon-layer-2.png"; // 76 x 76
  $watermarkFile = __DIR__ . "/stars-layer-3.png"; // 133 x 133
  
  // We want our final image to be 76x76 size
  $x = $y = 76;
  
  $final_img = imagecreate($x, $y); // where x and y are the dimensions of the final image
  
  $image_1 = imagecreatefrompng($bgFile);
  $image_2 = imagecreatefrompng($imageFile);
  $image_3 = imagecreatefrompng($watermarkFile);
  
  // Something going wrong here?
  imagealphablending($final_img, false);
  imagesavealpha($final_img, true);
  
  imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
  imagecopy($final_img, $image_2, 0, 0, 0, 0, $x, $y);
  imagecopy($image_3, $final_img, 0, 0, 0, 0, $x, $y);
  
  imagealphablending($final_img, false);
  imagesavealpha($final_img, true);
  
  ob_start();
  imagepng($final_img);
  $watermarkedImg = ob_get_contents(); // Capture the output
  ob_end_clean(); // Clear the output buffer
  
  header('Content-Type: image/png');
  echo $watermarkedImg; // outputs: `http://i.stack.imgur.com/yZJ6S.png`
  </code></pre>
  
  <p>I would like it to output something like: <a href="http://i.imgur.com/f7UWKA8.png" rel="nofollow">http://i.imgur.com/f7UWKA8.png</a> (a combination of the three images in order (background-icon-stars) with the correct colour).</p>