IMAGE COPY MERGE

This PHP function copies one image on to another and merges them to whatever parameter you assign. There are 9 parameters. See my other tute for more on the manual's descriptions. Here I will compare outputs with different types as well as effects of positioning. I will also bore you with all the technicalities necessary to get this script working properly...but hopefully it will make your PHP imaging experience more enjoyable.
I will be using a grid for the background and a small checkerboard for the foreground image. This will help show the location. For this first image I used a jpeg grid on to which I merged three small checkerboards, one is a jpg, one a gif and one a png.
grid
If you would like to check these images on your own server you can download the zip file which includes all the images and php script. There is also a png grid which you can use to substitute for the jpg grid if you'd like to see the effect. Below is the grid png background.
grid
Depending on what you are aiming for, different image types or combinations give different results. The script for the above jpeg grid is below. It shows the parameters used as well as the font script used to write on the image.
There are some specific rules PHP demands on you in order to make this script work properly. It is easy to lose sight of the rules when you are juggling images and numbers around to get the effect you want. Just remember these three rules and you should be OK.

  • The first variable (or name that you give to your first image) must be the first variable in the input script. In other words, as above $grid is the first variable name I gave to my first image. On the command line, imagecopymerge($grid.....is the first variable. Don't ever forget those semicolons at the end of each command line.
  • On the output line where you tell PHP to create your image, the first variable must be the name of the image. As above in the script: imagejpeg($grid);
  • Whatever header you decide to use (or image type), you must use the same type in the output line. As above: header('Content-type: image/jpeg'); matches the output type: imagejpeg($grid);
Now it does not matter if you use jpeg, png, or gif in your header, they all work. But if there is transparency, it must be either a png or a gif. Also the "e" in jpeg must be specified and not jpg. You can however merge a jpg image inside the script, but cannot make it a jpg in the output, only jpeg. If you are changing codes around and fail to change one of these lines to comply with your overall script, you may still get an image (depending on your server) but when you put the URL in your beamer to change it over to an image you will get a 404 error, file not found. Sometimes you will get an error such as "Kind of info that can't be used", and you go back in and find that your header says jpeg and your output says imagegif. Then you correct the error but the server still cannot produce the image on the reload. You can then try changing the file name completely so that the bogus script is no longer in its memory. This often will solve the problem.
Here are the three checkerboard images (created at Image Magick) in original form for comparison to their merged counterparts above.
checker
JPG
checkerboard
GIF
checkerboard
PNG
Here they are merged on to themselves.
imagecopymerge($name, $name, 0, 0, 0, 0, 100, 100, 50);
checkerboard
JPEG
checkerboard
GIF
checkerboard
PNG
OK enough with the technical stuff. Now let's look at the numbers, still bored? 8-)
I failed to find any official definition of "destination image" and "source image". But from working with this script I know the destination is the first and source is the second. Destination means target. The output name is the same as the first image so I guess the output is the target. Source means origin, so I assume the origin of the merge is from the second image. Below I have displayed the same two images with the numbers so that you can really see what is happening.
grid with parameters
The last number is the merge amount which goes from 0-100, 0 showing only the first image and 100 showing the second image sitting on top of the first.
Now for some tricks. If for your 5th and 6th parameters you used the Destination or first image, and if that image is larger than the source, you will get a shadowed out edge on the lower right hand corner as in this example.
image
Lastly you can use this function as a kind of cropper tool if you put the smaller image in first, as the destination image. Here is the grid merged on to a checkerboard at these parameters: ($checkerboard, $grid, 0, 0, 0, 0, 100, 100, 100);
grid
This will only chop off as much as the size of your smallest image. Sort of like the Trim function at IM. If you tried to merge at a number beyond the smaller image, again you will get the size of the smallest image. If the smaller of the two is the destination you can merge part of the smaller image and crop off the extra top in one script. Use ($checkerboard, $grid, 0, 0, 0, 0, 50, 50, 50);
image
See more on imagecopymerge by Sally
© LingoLinda.com