Some installations of PHP have the ability to create images on the fly. If you need to ask your server administrator, ask if “GD” or “ImageMagick” has been compiled into PHP. These examples assume the “GD” that usually comes built-in to PHP.
Create a new file, call it “graph.php”, and place the following code into it:
<?
$width = $_GET['width'];
$height = $_GET['height'];
$image = ImageCreate($width,$height);
$color = ImageColorAllocate($image,255,0,0);
ImageFilledRectangle($image,0,0,$width, $height,$color);
//send the image
header("content-type: image/png");
ImagePNG($image);
?>
The $_GET array contains only those $_REQUEST items that came from the URL. So when we call this page, we will need to specify the width and height as http://hostname/path/graph.php?width=xx&height=xx.
The GD image creation function ImageCreate() creates a blank image with the width and height we specify. In order to place color into the image, we need to also create the color, using ImageColorAllocate. We specify colors using three numbers. The numbers range from 0 to 255, and are for red, green, and blue. A high number in red and a low number in green and blue will give a red color, and so on.
We then fill the image with a rectangle that is as big as the image itself, using the red color we’ve created.
The important part of this is that we’re sending a header to tell the web browser that this is not a web page, it’s really an image. If you remember from the section on sessions, headers must be sent before you send any other data. If you get the “too late to send headers” error, make sure that there is no space or carriage return between the beginning of your document and the “<?” that starts the PHP code.
We’re not going to cover it here, but you can use the content-type header to send all sorts of document types. If you want dynamically-modified JavaScript, you can create it using PHP and send it with the content type “text/javascript”. Or you can send dynamically-modified style sheet with the content type “text/css”. (For JavaScript, you might use “application/x-javascript” instead. This is the technically correct content type. But the HTML 4.0 standard examples use text/javascript, so this is often what browsers support.)
Here we are sending it as “image/png”. PNG is the format of the image we are sending. We send our image with the ImagePNG function.
Now, change
FOREACH ($votecounts as $choice => $count):
$name = $pollchoices[trim($choice)];
echo "<p>$name received $count vote(s).</p>\n";
ENDFOREACH
to
echo "<table>\n";
FOREACH ($votecounts as $choice => $count):
$name = $pollchoices[trim($choice)];
IF (!$maximum):
$maximum = $count;
ENDIF;
$width = $count/$maximum*200;
echo "<tr><th align=\"right\">$name</th><td>";
echo "<img src=\"graph.php?height=40&width=$width\" />";
echo "($count)</td></tr>\n";
ENDFOREACH;
echo "</table>\n";
Our quote marks have to be backquoted so that PHP doesn’t think we’re ending the “echo” text.
You should get a graph of the vote totals that looks like:

In real life you wouldn’t use on-the-fly graphic creation for such a trivial task. You could more easily create a bar image and resize the bar using the <img> tag’s “width” option. But if you want to create a line graph or create a pie chart on the fly, you’ll find the image creation tools invaluable.