title: Merge two PNG images with PHP GD library
body: <p>Does anybody have a script which can merge two PNG images?</p>
  
  <p><strong>With the following conditions:</strong></p>
  
  <ul>
  <li>Both images have transparent areas</li>
  <li>The second image must have 50% opacity (<em>it is overlaid over the first image</em>)</li>
  </ul>
  
  <p><strong>Here is what I tried to do but without luck:</strong></p>
  
  <pre><code>&lt;?php
  
  function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
      $cut = imagecreatetruecolor($src_w, $src_h);
      imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 
      imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 
      imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
  }
  
  $image1 = imagecreatefrompng('a.png'); //300 x 300
  $image2 = imagecreatefrompng('b.png'); //150 x 150
  
  $merged_image = imagecreatetruecolor(300, 300);
  imagealphablending($merged_image, false);
  imagesavealpha($merged_image, true);
  
  imagecopy($merged_image, $image1, 0, 0, 0, 0, 300, 300);
  imagecopymerge_alpha($merged_image, $image2, 0, 0, 0, 0, 150, 150, 50);
  
  header('Content-Type: image/png');
  imagepng($merged_image);
  
  ?&gt;
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <ul>
  <li>First Image (left) and Second Image (right)</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/86JEj.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/pmjEZ.png" alt="enter image description here"></p>
  
  <ul>
  <li>This is how it should be (left) and the result of my code (right)</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/pkpKr.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/q8Zle.png" alt="enter image description here"></p>
  
  <ul>
  <li>The result of the solution proposed by <strong>dqhendricks</strong></li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/UEglk.png" alt="enter image description here"></p>