Specifications
3. Outputting the final graphic
4. Cleaning up resources
We’ll begin by looking at a very simple image creation script. This script is shown in
Listing 19.1.
LISTING 19.1 simplegraph.php —Outputs a Simple Line Graph with the Label Sales
<?
// set up image
$height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
// draw on image
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, “Sales”, $white);
// output image
Header (“Content-type: image/png”);
ImagePng ($im);
// clean up
ImageDestroy($im);
?>
The output from running this script is shown in Figure 19.1.
We’ll walk through the steps of creating this image one by one.
Creating a Canvas Image
To begin building or changing an image in PHP, you will need to create an image identifier.
There are two basic ways to do this. One is to create a blank canvas, which you can do with a
call to the ImageCreate() function, as we have done in this script with the following:
$im = ImageCreate($width, $height);
You need to pass two parameters to ImageCreate(). The first is the width of the new image,
and the second is the height of the new image. The function will return an identifier for the
new image. (These work a lot like file handles.)
Generating Images
C
HAPTER 19
19
G
ENERATING
IMAGES
405
24 7842 CH19 3/6/01 3:42 PM Page 405










