ImageCopy and ImageCopyMerge

This tutorial will cover compositing 2 images with PHP and GD. ImageCopy will place one image on top of the other. ImageCopyMerge will also place one image on top of another with the exception that you can merge the two so that the bottom image is showing through. I'll be working with these 2 images. The nesting dolls background is transparent.


To center the dolls on the background, first you must use imagecreatefromgif() for each image, and copy one on to the other with the following line:
The background image $brick goes in first, the top image $dolls goes in second. The first 2 numbers in that line 15,1 indicate where on the back image I want to begin to copy. Since I want to center I need to calculate where the center is. The bricks are 230 pixels wide while the dolls are 200 pixels wide, 30/2=15, so I need to move the top image in by 15 pixels. They are the same height so I do not need to center the height. The second set of numbers, 0,0 indicate the top left corner of the top image. I want the whole image. The third set of numbers, 0,200 indicate the bottom right corner of the top image which is basically the width and height of the top image.
Notice that the color changes on the gif doll image.
Right click, select all, ctrl+c to copy.
Note that I used imagepng($brick) to create the image as that is the main canvas of the image.
A cleaner script is to create an image canvas first using imagecreatetruecolor, compositing the background image onto the truecolor canvas and then the top image on top of that. Since it is a gif (which cannot be used with truecolor images), I added imagetruecolortopalette. Now the colors are true.
If I only want to copy the second doll onto the center, then I would use the following line.
65,0 is where I want to place it on the back (doll width 200/2=100, back 230-100=130, 130/2=65). 100,0 is where I want to start copying the top image. 100,180 is what is left of the top image after I started the copy.
Right click, select all, ctrl+c to copy. Here I cropped off the back by creating a blank image canvas of the size I needed to composite only 1 doll. Then I copied the back onto it, cutting off the right side of the image. Then copied the doll onto that image.
imagecopymerge uses the same principal except that one more number is added to the sequence for percentage of blend from 0 no action to 100 same as imagecopy.
This is the only line that changes in the above script. I've changed imagecopy to imagecopymerger and added the percentage.