Backup of the original page by Frank Boumphrey:
www.hypermedic.com/php/index.htm
Please consult the original if still available.
PHP allows us to Manipulate Graphics 'on-the-fly' It can handle two graphics formats, JPEG, and PNG. Early versions of PHP also supported Gif, but these graphics are copyrighted by Unisys, and they started making noises about charging web users. Obviously for a language that was open, this was unacceptable. Not to worry though, the PNG format does just about everything that the Gif format does, and in most cases does it better. All modern browsers support PNG graphics.
In order to draw a picture in PHP we have to do the following.
When we create a canvas, we are in fact setting aside memory in the computer that we can use to draw on. To do this we use the ImageCreate([int],[int])
function which takes two arguments which represent the width and the height
of file of the canvas we will be using. It returns a resource integer, which
we then use as a handle to refer to all operations we carry out on the canvas.
$Image=ImageCreate(400,200);
The above code has created a canvas in memory 400 Pixels wide, and 200 Pixels high.
We
will go into all the details of drawing (or rather we will cover some of
them), but assuming that you have drawn something, you will either want to
output it to a browser, or you will want to save it to disc. This is done
using either the ImagePNG(), function, or the ImageJPEG()
function. These functions both take a required and an optional parameter.
The required parameter is image handle, and the optional parameter is a file
path where we will store the file.
The syntax is quite simple:
$Image=ImageCreate(400,200);
//...draw on the canvas
ImagePNG($Image); //outputs the image to the browser.
Simply add the full path name of the destination. Add as many destinations as you want.
$Image=ImageCreate(400,200);
//...draw on the canvas
ImagePNG($Image,"graphic6.png"); //writes image to same folder as program
ImagePNG($Image,"c:/courses/php/graphic6.png"); //writes the image to this address.
Note that the function does either one or the other, if we want to both write out the file, and stored it to disc we must call the function twice.
All
operations with Graphics tend to be memory intensive. Therefore it is vital
that we free up memory after finishing drawing by destroying our canvas.
We use the ImageDestroy([image handle]) function which takes as an argument the image handle to do this.
ImageDestroy($Image);
The canvas we created in the last section is now destroyed
Now we've seen how to create and destroy our canvas, let's find out how to draw on it.
Whenever we refer to our canvas we will use x, and y coordinates. The top left corner of the canvas is 0, 0. The bottom right (of the canvas we created in the first section) is 400,200. All measurements are in Pixels.
In order to use a color we have to create a color using the ImageColorAllocate([int red],[int green],[int blue]) function and pass it to a color handle.
The function takes three integers between 0 and 256 as parameters. These
represent the amount of red, green, and blue coloring respectively.
$[color handle]=ImageColorAllocate([int red],[int green],[int blue])
For those not familiar with this concept, all colors are made by mixing the three primary colors, red, green, and blue. What you are doing in CSS or HTML when you assign a color (e.g. bright yellow #ffff00) is using hexadecimal numbers between 0 and 250. In PHP we use decimal numbers separated by a commas to do the same thing.
The following code will create a bright yellow color which we pass to a handle we choose to call canary
$canary=ImageColorAllocate(255,255,0)
ImageLine($[Image handle],[x1],[y1],[x2],[y2],[color handle]); function.
Let's leap right in a draw a line! Type up the following in your favorite editor. The code will draw a red line on a green background, 10 pixels wide diagonally across the canvas.
<? 1 header("content-type:image/png"); 2 $Image=ImageCreate(400,200); 3 $green=ImageColorAllocate($Image,0,255,0); 4 $red=ImageColorAllocate($Image,255,0,0); 5 imagesetthickness($Image,5); 6 Imageline($Image,10,10,200,150,$red); 7 ImagePNG($Image); 8 ImageDestroy($Image); ?>
header() to send our content type. If you leave this out, the browser will treat it as a text file. (For fun, replace this line with echo "hh" and see what happens!imagesetthickness($Image,5); takes two arguments, the image-thickness and the width we want to draw our line. The width is measured in PixelsHere is the graphic we have just created.

To draw circles and ellipses use the ImageArc($[Image handle],[x1],[y1],[width],[height], [start],[finish],[color handle])
function. The following graphic shows how to interpret these parameters.
Note especially that x and y is the center point of the ellipse.
If we set start to 0 and end to 360, a complete eclipse will be drawn, other wise only an arc will be drawn. If we set the height and the width to the same values a circle will be drawn.
In the following example we add several circles to our graphic, and flood-fill them.
<? 1 header("content-type:image/png"); 2 $Image=ImageCreate(400,200); 3 $green=ImageColorAllocate($Image,0,255,0); 4 $red=ImageColorAllocate($Image,255,0,0); 5 $yellow=ImageColorAllocate($Image,255,255,0); 6 $black=ImageColorAllocate($Image,0,0,0); 7 $blue=ImageColorAllocate($Image,0,0,255); 8 imagesetthickness($Image,5); 9 ImageArc($Image,200,100,180,180,0,360,$black); 10 ImageArc($Image,160,80,25,25,0,360,$black); 11 ImageArc($Image,240,80,25,25,0,360,$black); 12 imagefill ($Image, 200, 100, $yellow); 13 imagefill ($Image, 160, 80, $blue); 14 imagefill ($Image, 240, 80, $blue); 15 imagesetthickness($Image,10); 16 ImageArc($Image,200,100,120,130,45,135,$red); 17 Imageline($Image,0,0,400,200,$red); 18 ImagePNG($Image); 19 //ImagePNG($Image,"graphic2.png"); 20 ImageDestroy($Image); ?>
We will just go over the new code.
header() to send our content type.*The imagefill([image handle],[x],[y],[color handle]) takes these four parameters. It will create a flood fill starting at the coordinates x and y.
Here is the resulting image.
Squares are drawn using the ImageRectangle([Image handle],[x1,[y1],[x2],[y2],[color handle]) function. In the following code we place a 10 px thick black border around our image by adding a single line of code.
<?
header("content-type:image/png");
$Image=ImageCreate(400,200);
$green=ImageColorAllocate($Image,0,255,0);
$red=ImageColorAllocate($Image,255,0,0);
$yellow=ImageColorAllocate($Image,255,255,0);
$black=ImageColorAllocate($Image,0,0,0);
$blue=ImageColorAllocate($Image,0,0,255);
imagesetthickness($Image,5);
ImageArc($Image,200,100,180,180,0,360,$black);
ImageArc($Image,160,80,25,25,0,360,$black);
ImageArc($Image,240,80,25,25,0,360,$black);
imagefill ($Image, 200, 100, $yellow);
imagefill ($Image, 160, 80, $blue);
imagefill ($Image, 240, 80, $blue);
imagesetthickness($Image,10);
ImageArc($Image,200,100,120,130,45,135,$red);
Imageline($Image,0,0,400,200,$red);
ImageRectangle($Image,0,0,400,200,$black);
ImagePNG($Image);
//ImagePNG($Image,"graphic3.png");
ImageDestroy($Image);
?>
Here is the result.
PHP has some sophisticated methods for writing graphical text. You can
use all kinds of Fonts, but if you do this you will have to load or enable
several files on your server. You are directed to the manual for full details.
In the example below, We will content our selves with using the default font.
The function used is the ImageString() function. The syntax is given below.
imagestring ([image handle], [font handle], [x], [y], "[string]", [color Handle]);
The font handle needs a little more explanation. You can declare a path to a font using special functions, and this will be stored in an Integer. However as we are using the default PHP graphic font, we will just use an integer between 1 (smallest) and 5 (largest).
Note also that x and y are the coordinates of the top left corner of the image string.
<?
header("content-type:image/png");
$Image=ImageCreate(400,250);
$green=ImageColorAllocate($Image,0,255,0);
$red=ImageColorAllocate($Image,255,0,0);
$yellow=ImageColorAllocate($Image,255,255,0);
$black=ImageColorAllocate($Image,0,0,0);
$blue=ImageColorAllocate($Image,0,0,255);
imagesetthickness($Image,5);
ImageArc($Image,200,100,180,180,0,360,$black);
ImageArc($Image,160,80,25,25,0,360,$black);
ImageArc($Image,240,80,25,25,0,360,$black);
imagefill ($Image, 200, 100, $yellow);
imagefill ($Image, 160, 80, $blue);
imagefill ($Image, 240, 80, $blue);
imagesetthickness($Image,10);
ImageArc($Image,200,100,120,130,45,135,$red);
Imageline($Image,0,0,400,200,$red);
imagesetthickness($Image,6);
ImageRectangle($Image,3,3,397,200,$black);
imagestring ($Image, 5, 130, 215, "NO MORE SMILEYS!", $black);
ImagePNG($Image);
//ImagePNG($Image,"c:/courses/php/graphic4.png");
ImageDestroy($Image);
?>
Again we will just go over the new code.
Here is the result.
In order to write Vertical text we use the imagestringup() function. It takes the same parameters as the imagestring() function. Note that somewhat perversely the x/y coordinates mark the bottom left corner of the text image.
<?
header("content-type:image/png");
$Image=ImageCreate(440,200);
$green=ImageColorAllocate($Image,0,255,0);
$red=ImageColorAllocate($Image,255,0,0);
$yellow=ImageColorAllocate($Image,255,255,0);
$black=ImageColorAllocate($Image,0,0,0);
$blue=ImageColorAllocate($Image,0,0,255);
imagesetthickness($Image,5);
ImageArc($Image,200,100,180,180,0,360,$black);
ImageArc($Image,160,80,25,25,0,360,$black);
ImageArc($Image,240,80,25,25,0,360,$black);
imagefill ($Image, 200, 100, $yellow);
imagefill ($Image, 160, 80, $blue);
imagefill ($Image, 240, 80, $blue);
imagesetthickness($Image,10);
ImageArc($Image,200,100,120,130,45,135,$red);
Imageline($Image,0,0,400,200,$red);
imagesetthickness($Image,6);
ImageRectangle($Image,3,3,397,200,$black);
//$myFont=ImageLoadFont("C:\WINNT\FONTS\coure.fon");
//ImageTTFText($Image, 20, 0, 10, 20, $black, "ARIAL.TTF","Testing");
imagestringup($Image, 5, 410, 160, "NO MORE SMILEYS!", $black);
ImagePNG($Image);
ImagePNG($Image,"c:/courses/php/graphic5.png");
ImageDestroy($Image);
?>
No fresh explanation is need. We just widened our canvas, and placed our graphical text in it. Here is the result.
We can create sophisticated polygonal shapes using the imagepolygon (); function. Here is the syntax:
imagepolygon ([Image Handle], [Array of vertices coords], [Number of Vertices], [color handle]);
Note that the array is of the x/y coordinates.
$array[0]=x1;
$array[1]=y1;
$array[2]=x2;
$array[3]=y2;
Etc.
The code below creates a red star on a yellow background.
<? 1 $Image=ImageCreate(400,300); 2 $yellow=ImageColorAllocate($Image,255,255,0); 3 $red=ImageColorAllocate($Image,255,0,0); 4 $black=ImageColorAllocate($Image,0,0,0); 5 $polypoints[0]=200; //x1 6 $polypoints[1]=20; //y1 7 $polypoints[2]=235; //x2 8 $polypoints[3]=125; //y2 9 // the polygon array 10 $polypoints[4]=350; //x3 11 $polypoints[5]=125; //y3 12 $polypoints[6]=252; //x4 13 $polypoints[7]=185; //y4 14 $polypoints[8]=288; //x5 15 $polypoints[9]=280; //y5 16 $polypoints[10]=200; //x6 17 $polypoints[11]=220; //y6 18 $polypoints[12]=112; //x7 19 $polypoints[13]=280; //y7 20 $polypoints[14]=148; //x8 21 $polypoints[15]=185; //y8 22 $polypoints[16]=50; //x9 23 $polypoints[17]=125; //y9 24 $polypoints[18]=165; //x10 25 $polypoints[19]=125; //y10 26 ImageSetThickness($Image,5); 27 imagepolygon ($Image, $polypoints, 10, $black); 28 imagefill ($Image, 200, 100, $red); 29 ImagePNG($Image,"c:/courses/php/graphic6.png"); 30 ImagePNG($Image); 31 ImageDestroy($Image); ?>
We will just go over the new code.
Here is the result.
You may have noticed that in all the preceding example we have not been using any html. If we did we would just get a bunch of gibberish instead of a graphic! This may appear to limit the usefulness of the php graphical methods. However we can get round this by writing the mage to file and then viewing it using the <img /> element.
<?
$Image=ImageCreate(400,200);
$green=ImageColorAllocate($Image,0,255,0);
$red=ImageColorAllocate($Image,255,0,0);
ImageSetThickness($Image,10);
ImageLine($Image,0,0,400,200,$red);
ImagePNG($Image,"Image.png");
ImageDestroy($Image);
?>
<html>
<title>Saved image</title>
<p>Here's our image!</p>
<img src="Image.png" />
</html>
Another way to get round this problem is to use an HTML 'frames' application, and this is what we will do in our "Pie Chart" case study!
İFrank Boumphrey 2001