title: PHP GD Use one image to mask another image, including transparency
body: <p>I am trying to create a PHP script that takes an image:</p>
  
  <p><img src="https://i.stack.imgur.com/WJuvq.png" alt="image1"><br>
  <a href="https://i.stack.imgur.com/eNvlM.png" rel="noreferrer">http://i.stack.imgur.com/eNvlM.png</a> </p>
  
  <p>and then applies a PNG image:</p>
  
  <p><img src="https://i.stack.imgur.com/GYHwi.png" alt="mask"><br>
  <a href="https://i.stack.imgur.com/iJr2I.png" rel="noreferrer">http://i.stack.imgur.com/iJr2I.png</a></p>
  
  <p>as a mask.</p>
  
  <p>The end result needs to maintain transparency:</p>
  
  <p><img src="https://i.stack.imgur.com/9snqE.png" alt="result"><br>
  <a href="https://i.stack.imgur.com/u0l0I.png" rel="noreferrer">http://i.stack.imgur.com/u0l0I.png</a></p>
  
  <p>If at all possible I want to do this in GD, ImageMagick is not really an option right now.</p>
  
  <p>How would I go about this? </p>
  
  <p><em><a href="https://stackoverflow.com/questions/1056104/php-gd-how-to-copy-a-circle-from-one-image-to-another/1056117#1056117">phalacee's post (in "PHP/GD, how to copy a circle from one image to another?")</a> seems to be along the right lines but I specifically need to use an image as a mask, not a shape.</em></p>