The imagecopymerge function accepts the base image, the overlay image, coordinates where to place the overlay on the base image, cropping coordinates for the overlaid section and an alpha transparency.
The overlaid image should be positioned at the top left of the base image. Since the entire original image will be superimposed, it's cropped coordinates should be 0,0 to its width and height. The image should be fully opaque so a value of 100 should be passed for the alpha.
The final results can then be sent to the browser using imagejpeg.
By sending a Content-Type header, we are creating a PHP script that outputs the jpeg file directly to the browser.
The imagejpeg function can either save the image to a file or send the image directly to the request. By specifying an empty string for the file name it knows we wish to output the image directly to the browser. You can also set the compression quality of the image, which I have set at 100.
Once the image has been sent it's good practice to free up the resources we've consumed to generate it.