How To Generate The Image
Now that I’ve kept you waiting for so long, on to create that image!
The source code below shows the general stub for creating an image in PHP.
The source code below shows the general stub for creating an image in PHP.
Let’s define the image’s
since you’ll probably want to include those in the calling HTML
Next, we allocate some memory for drawing the image using the
function. Also, we need to allocate all the colors we are going to use.
The first color to be allocated will be the background color of the image by default.
The RGB color values can also be given in decimal numbers, instead of the hex I use below.
$width
and $height
first,since you’ll probably want to include those in the calling HTML
IMG
tag.Next, we allocate some memory for drawing the image using the
imagecreate()
function. Also, we need to allocate all the colors we are going to use.
The first color to be allocated will be the background color of the image by default.
The RGB color values can also be given in decimal numbers, instead of the hex I use below.
Before sending the image out, we must include an HTTP header telling the receiving
browser how to interpret the binary data it gets thrown at.
Finally,
and
browser how to interpret the binary data it gets thrown at.
Finally,
imagegif()
outputs the image over the netand
imagedestroy()
frees up the internal memory we reserved in the beginning.
<?php
/* chart.php */
$width = 480;
$height = 250;
$image = imagecreate($width, $height);
// colors
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
// draw something here
// flush image
header("Content-type: image/gif"); // or "Content-type: image/png"
imagegif($image); // or imagepng($image)
imagedestroy($image);
?>
If you want to use the PNG image format instead of GIF,
simply change the
replace
is to use the
argument. This will save the image to the file in
to the browser. We’ll use this later for caching the image.
simply change the
Content-type
to image/png
andreplace
imagegif()
by imagepng()
. A final trickis to use the
imagegif/imagepng($image, $path)
functions with a secondargument. This will save the image to the file in
$path
, instead of sending itto the browser. We’ll use this later for caching the image.
Layout And Borders
As of now, the image is disappointingly empty. Let’s assume you want to plot a time
series with 12 values. For example, a fictional president’s approval ratings throughout
one year. A public relations disaster hits sometime in April and it took the spin doctors
a full 6 months and everybody’s Christmas mood to restablish some confidence in his
leadership. This is what the data might look like:
series with 12 values. For example, a fictional president’s approval ratings throughout
one year. A public relations disaster hits sometime in April and it took the spin doctors
a full 6 months and everybody’s Christmas mood to restablish some confidence in his
leadership. This is what the data might look like:
<?php
$data
= array(
"Jan" => 55,
"Feb" => 54,
"Mar" => 53,
"Apr" => 33,
"May" => 13,
"Jun" => 15,
"Jul" => 23,
"Aug" => 28,
"Sep" => 32,
"Oct" => 45,
"Nov" => 73,
"Dec" => 71);
?>
We can easily get the maximum value to plot – 73 – and the number of
elements – 12 – from this array using
This will allow us to scale the chart to the size of the image. But first, we’ll have to
decide on the general layout.
elements – 12 – from this array using
max()
and sizeof()
.This will allow us to scale the chart to the size of the image. But first, we’ll have to
decide on the general layout.
We need a vertical margin to leave some space both for a title on top and for X axis
labels on the bottom. A horizontal margin will make room on the left-hand side for the
Y axis labels. This leaves an area of
for the chart.
labels on the bottom. A horizontal margin will make room on the left-hand side for the
Y axis labels. This leaves an area of
$xsize
by $ysize
pixelsfor the chart.
In the code sniplet below, we use
the chart area. Note that X and Y coordinates are counted from the upper left corner
which is (0, 0).
imagerectangle()
to draw a frame forthe chart area. Note that X and Y coordinates are counted from the upper left corner
which is (0, 0).
<?php
$maxval
= max($data);
$nval = sizeof($data);
$vmargin = 20; // top (bottom) vertical margin for title (x-labels)
$hmargin = 38; // left horizontal margin for y-labels
$base = floor(($width - $hmargin) / $nval); // distance between columns
$ysize = $height - 2 * $vmargin; // y-size of plot
$xsize = $nval * $base; // x-size of plot
// plot frame
imagerectangle($image, $hmargin, $vmargin,
$hmargin + $xsize, $vmargin + $ysize, $black);
?>