Image Copy with PHP and GD
Lesson I: For absolute beginners

These tutes will be small exercises in using gradient images to create a php file that can be converted in to a jpg image. Each lesson will build on to the next lesson to show different techniques and codes that can be used. For this first lesson I will be using 5 jpg images: 4 gradients and a gradpal.

First some information about using PHP, GD and imagecopy. You must have access to PHP scripting and GD on your server. You can check with your Administrator if you are not sure.

The following set of PHP codes will be used in every file. They are required. They speak to the server telling it what you want to do.

  1. <?php
  2. header("Content-type: image/jpeg");
  3. $width = 500;
  4. $height = 500;
  5. $im = imagecreatetruecolor($width, $height);

  7. Imagejpeg ($im);
  8. imagedestroy($im);
  9. ?>
Explanation of above codes
  1. The opening php code tells the server it is a php script. While a php script can be inserted in to an HTML file, like a javascript script, a GD script cannot. It must stand alone in a file with no html whatsoever.
  2. The header says that we want to create a jpeg. There are also the options of gif and png but for this lesson we will use jpeg.
  3. The width of the background
  4. and height are whatever size you wish, in pixels.
  5. The $im = imagecreatetruecolor tells the server to create a true color palate image of the designated W and H (in that order). You can leave those two variables out (#s 3 and 4) and just insert the numbers on that line, as an alternative. $im = imagecreatetruecolor(500, 500). The $im stands for image. It is the most common variable used, but you can use any name you choose.
  6. Here is where we begin to build the image.
  7. Finally, Imagejpeg ($im); tells the server to create the image.
  8. The last line, imagedestroy($im); tells the server that I am through with this script and frees the image from its memory/cache.
  9. Last the closing php script.

For this first lesson this is the image I am going to put together with php imagecopy. Here are the five images I will be using. You can upload those or choose alternatives:

Here are some images to choose from:
Reversed Angles
White angles

Upload your choice of images to your directory where your php script will be. The next set of codes tells the server what images to use and what their variable names are. You can name them whatever you want. Be sure to use a dollar sign preceding the name. Also be sure to use a semicolon at the end of every line of code. The line "imagecreatefromjpeg()" is a php command and must be used for each image. The URL of your image is placed in the parenthesis with single quotes (can use double but single is a good habit as in more complicated PHP scripts you have nested quotes with single quotes inside a line with double quotes around it).

$G140 = imagecreatefromjpeg ('G140.jpg');
$Gn140 = imagecreatefromjpeg ('Gn140.jpg');
$B140 = imagecreatefromjpeg ('B140.jpg');
$Bn140 = imagecreatefromjpeg ('Bn140.jpg');
$mod = imagecreatefromjpeg('gradMod2.jpg');

These images too will also need to be destroyed when you are finished with them, at the end of the script, under this line of code just repeat the code for each image:


Now come the imagecopy codes which is what builds the actual image. Basically what you need to use for parameters are:

(destination, source, X, Y of back, X, Y of top, W, H of top)

In other words, the X, Y of where on the background you would like to place your image, the X, Y of the part of the image that you are placing on top along with the W, H of the top image. Briefly this chart shows the X, Y of a 50x50 image as 0, 0, the beginning pixel (top left) and 50, 50, is the W, H (bottom right).


Here are the codes I used for my image:

imagecopy($im, $mod, 135, 60, 0, 0, 50, 189);
imagecopy($im, $Bn140, 110, 250, 0, 0, 50, 50);
imagecopy($im, $B140, 160, 250, 0, 0, 50, 50);
imagecopy($im, $G140, 110, 300, 0, 0, 50, 50);
imagecopy($im, $Gn140, 160, 300, 0, 0, 50, 50);

Looking at the first line:
imagecopy() is a PHP command. $im is the background, $mod is the girl image, 135 is the distance from the left on the background where I want to place the image, 60 is the distance from the top on the background, 0, 0, is the top left corner of the girl image and 50, 189 is the W, H of the girl image. As far as trying to decide where on the page to put the image it is sometimes easiest to decide where the center is and work around that to ensure that you have enough space to the left and right of the page as you keep adding more images. Then trim the extra space off at IM when you are through. If you know exactly how large your final image will be you can figure out the number for that and make the width and height of your background so that there will be nothing to trim off your final image. I have done this image both ways and simple math should tell you what the numbers are.

Here are the Codes for the final image, all put together. Here are the Codes again for the same image with no background to trim off.

You can CCP the codes, but understanding them is the key to future success. I don't expect you to memorize them right off. I still have to go back in to old scripts to CCP.

Now your php script is ready to be made into a jpg. There are three ways to do this.

  1. You can put the URL of your php file in to your uploader/mover on your server. When it asks for a new name change the php extension to jpg
  2. You can script the output in to your php script so that it automatically loads the jpg in to your directory when you click on the php file. You may get a pop-up saying that it's a kind of information that can't be used or your server may seem to not do anything at all. You may also have to reload the directory for the image file to appear. This command is done on the

    Imagejpeg ($im);

    line by placing the jpg name inside parentheses and single quotes next to the $im with a comma as follows:

    Imagejpeg ($im, ('PalsWorld.jpg'));

  3. You can also take your PHP url to image magick and place it in either the slot marked filename or the one marked URL on the input page, then hit the View button. It will load in to the studio just like an image where you can trim or resize or whatever you want to do there and then output it as a jpg. If you have IM on an F key you can hit the F key while viewing your php image page and it will load.
    1. That was a lot of information for a short tute but there was a lot to cover. The following lessons should be shorter and easier, once you've got the basics. The second lesson will look at gif and png manipulations using this first php script.

      LESSON 2