16.11. Basic Image CopyingThe two functions imagecopy( ) and imagecopymerge( ) are similar in that they copy one picture into another. Both of their first eight parameters are identical:
Parameters three and four allow you to position the source image where you want it on the destination image, and parameters five, six, seven, and eight allow you to define the rectangular area of the source image that you want to copy. Most of the time, you will want to leave parameters five and six at 0 (copy from the top-left corner of the image), and parameters seven and eight at the width of the source image (the bottom-right corner of it) so that it copies the entire source image. The way these functions differ is in the last parameter: imagecopy( ) always overwrites all the pixels in the destination with those of the source, whereas imagecopymerge( ) merges the destination pixels with the source pixels by the amount specified in the extra parameter: 0 means "Keep the source picture fully," 100 means "Overwrite with the source picture fully," and 50 means "Mix the source and destination pixel colors equally." The imagecopy( ) function is therefore equivalent to calling imagecopymerge( ) and passing in 100 as the last parameter. Figures 16-17 and 16-18 show two input images that will be used to test these functions. Figure 16-17. Our source picture: some starsNow, to get those two to merge, we need a script like this one: $stars = imagecreatefrompng("stars.png"); $gradient = imagecreatefrompng("gradient.png"); imagecopymerge($stars, $gradient, 0, 0, 0, 0, 256, 256, 60); header('Content-type: image/png'); imagepng($stars); imagedestroy($stars); imagedestroy($gradient); That merges the two at 60%, which gives slightly more prominence to the gradient. The result is shown in Figure 16-19. Figure 16-18. Our destination picture: a smooth, blue gradientFigure 16-19. Stars + gradient + some imagination = the night sky |