digitalpoint
Mar 22nd 2004, 11:10 am
One of the really neat things about PHP (one of the many) is the ability to dynamically generate images by using the GD lib (although it's compiled in by default, so you don't need to know anything about it).
A simple example would be to display Yahoo WebRank graphic (http://www.digitalpoint.com/tools/webrank/graphic.php?wr=4). The wr= part of the URL lets you get an image that ranges from 0-10 and display the bar graph.
So how is this done?
I made it easy on myself and made 3 different images that PHP pieces together. The background image, the bar part of the graph, and the top of the bar graph.
The graphic.php file:
<?php
$im = ImageCreateFromPNG ("bg.png");
$bar = ImageCreateFromPNG ("bar.png");
$bartop = ImageCreateFromPNG ("bartop.png");
for ($i = 1; $i <= $wr; $i++) {
ImageCopy ($im, $bar, 4, 62 - (6 * $i), 0, 0, 40, 6);
}
if ($wr > 0) ImageCopy ($im, $bartop, 4, 62 - (6 * $wr), 0, 0, 40, 4);
$col_mgrey = ImageColorAllocate ($im, 100, 100, 100);
$col_lgrey = ImageColorAllocate ($im, 200, 200, 200);
ImageString($im, 5, 20, 4, $wr, $col_mgrey);
ImageString($im, 5, 21, 5, $wr, $col_lgrey);
Header ("Content-type: image/png");
ImagePng ($im);
ImageDestroy ($bar);
ImageDestroy ($bartop);
ImageDestroy ($im);
?>
I won't get into complex image creation, but it can be done as well... an example is the attachment, which shows new users per day (from the keyword tracker) as of right now, with a unique line per language. Another interesting thing about the way that one is done is a SQL handle is fed to the function, so anything you can query with a SQL statement can be graphed.
- Shawn
A simple example would be to display Yahoo WebRank graphic (http://www.digitalpoint.com/tools/webrank/graphic.php?wr=4). The wr= part of the URL lets you get an image that ranges from 0-10 and display the bar graph.
So how is this done?
I made it easy on myself and made 3 different images that PHP pieces together. The background image, the bar part of the graph, and the top of the bar graph.
The graphic.php file:
<?php
$im = ImageCreateFromPNG ("bg.png");
$bar = ImageCreateFromPNG ("bar.png");
$bartop = ImageCreateFromPNG ("bartop.png");
for ($i = 1; $i <= $wr; $i++) {
ImageCopy ($im, $bar, 4, 62 - (6 * $i), 0, 0, 40, 6);
}
if ($wr > 0) ImageCopy ($im, $bartop, 4, 62 - (6 * $wr), 0, 0, 40, 4);
$col_mgrey = ImageColorAllocate ($im, 100, 100, 100);
$col_lgrey = ImageColorAllocate ($im, 200, 200, 200);
ImageString($im, 5, 20, 4, $wr, $col_mgrey);
ImageString($im, 5, 21, 5, $wr, $col_lgrey);
Header ("Content-type: image/png");
ImagePng ($im);
ImageDestroy ($bar);
ImageDestroy ($bartop);
ImageDestroy ($im);
?>
I won't get into complex image creation, but it can be done as well... an example is the attachment, which shows new users per day (from the keyword tracker) as of right now, with a unique line per language. Another interesting thing about the way that one is done is a SQL handle is fed to the function, so anything you can query with a SQL statement can be graphed.
- Shawn