The basic PHP function used to create a new image is called ImageCreate(), but creating an image is not as simple as just calling the function. Creating an image is a stepwise process and includes the use of several different PHP functions.
Creating an image begins with the ImageCreate() function, but all this function does is set aside a canvas area for your new image. The following line creates a drawing area that is 150 pixels wide by 150 pixels high:
$myImage = ImageCreate(150,150);
With a canvas now defined, you should next define a few colors for use in that new image. The following examples define five such colors, using the ImageColorAllocate() function and RGB values:
$black = ImageColorAllocate($myImage, 0, 0, 0); $white = ImageColorAllocate($myImage, 255, 255, 255); $red = ImageColorAllocate($myImage, 255, 0, 0); $green = ImageColorAllocate($myImage, 0, 255, 0); $blue = ImageColorAllocate($myImage, 0, 0, 255);
By the Way
In your script, the first color you allocate is used as the background color of the image. In this case, the background color will be black.
Now that you know the basics of setting up your drawing canvas and allocated colors, you can move on to learning to draw shapes and actually output the image to a web browser.
Drawing Shapes and Lines
Several PHP functions can assist you in drawing shapes and lines on your canvas:
Using these functions requires a bit of thinking ahead because you must set up the points where you want to start and stop the drawing that occurs. Each of these functions uses x-axis and y-axis coordinates as indicators of where to start drawing on the canvas. You must also define how far along the x-axis and y-axis you want the drawing to occur.
For example, the following line draws a rectangle on the canvas beginning at point (15,15) and continuing on for 40 pixels horizontally and 70 pixels vertically, so that the lines end at point (55,85). Additionally, the lines will be drawn with the color red, which has already been defined in the variable $red.
ImageRectangle($myImage, 15, 15, 55, 85, $red);
If you want to draw another rectangle of the same size but with white lines, beginning at the point where the previous rectangle stopped, you would use this code:
ImageRectangle($myImage, 55, 85, 125, 135, $white);
Listing 14.1 shows the image-creation script so far, with a few more lines added to output the image to the web browser.
Listing 14.1. Creating a New Image
Lines 1718 output the stream of image data to the web browser by first sending the appropriate header() function, using the MIME type of the image being created. Then you use the ImageGif(), ImageJpeg(), or ImagePng() functions as appropriate to output the data stream; this example outputs a PNG file. In line 21, we use the ImageDestroy() function to clear up the memory used by the ImageCreate() function at the beginning of the script.
Save this listing as imagecreate.php and place it in the document root of your web server. When accessed, it should look something like Figure 14.1, only in color.
Figure 14.1. A canvas with two drawn rectangles.
Using a Color Fill
The output of Listing 14.1 produced only outlines of rectangles. PHP has image functions designed to fill areas as well:
These functions are used just like their nonfill counterparts. In Listing 14.2, the nonfill functions are replaced with functions designed to fill an area. In other words, only lines 13 and 14 have changed.
Listing 14.2. Creating a New Image with Color Fills
Save this listing as imagecreatefill.php and place it in the document root of your web server. When accessed, it should look something like Figure 14.2, but again, in color.
Figure 14.2. A canvas with two drawn and filled rectangles.