I am trying to create a PHP script that takes an image:

image1
https://i.sstatic.net/eNvlM.png

and then applies a PNG image:

mask
https://i.sstatic.net/iJr2I.png

as a mask.

The end result needs to maintain transparency:

result
https://i.sstatic.net/u0l0I.png

If at all possible I want to do this in GD, ImageMagick is not really an option right now.

How would I go about this?

phalacee's post (in "PHP/GD, how to copy a circle from one image to another?") seems to be along the right lines but I specifically need to use an image as a mask, not a shape.

Edit
Report