PHP can do a lot. Did you know that besides outputting HTML and interfacing with our databases, if you have the Graphics Draw library installed (most servers do), you can write code to create images on the fly? Take this one step further and PHP can take the data from your database and create charts from your queries dynamically! I think this is simply fantastic!
You can see the code I wrote here using PHP’s GD Library:
header('Content-type: image/png');
$png_image = imagecreate(300, 300);
$red = imagecolorallocate($png_image, 230, 65, 79);
$blue = imagecolorallocate($png_image, 15, 50, 117);
$white = imagecolorallocate($png_image, 255, 255, 255);
imagefilltoborder($png_image, 0, 0, $white, $white);
imagefilledellipse ($png_image, 150, 150, 300, 300, $red);
imagefilledellipse ($png_image, 150, 150, 250, 250, $white);
imagefilledellipse ($png_image, 150, 150, 200, 200, $red);
imagefilledellipse ($png_image, 150, 150, 130, 130, $blue);
$poly_points = array(150, 85, 130,120, 92, 125, 120, 155, 105, 192, 150, 175, 195, 192, 180,155, 208,125, 170,120);
imagefilledpolygon ($png_image, $poly_points, 10, $white); // POLYGON - 5 pointed star (needs 10 x,y coords)
imagepng($png_image);
imagedestroy($png_image);
I did this merely as a personal exercise so that I can teach how to use the GD library for the purposes of visualizing data in the form of charts, which I think can be quite useful. The best way to do that is of course keeping the file as a php file to be rendered as a png file upon viewing. Maybe I’ll write a tutorial on that specifically.
I found this great resource: http://www.phpforkids.com/php/php-gd-library-drawing-shapes.php and experimented. (BTW I love that there’s a website called PHP For Kids) That’s how I created the image above with code. Not the most efficient way to create an image, but if you need to feed those numbers dynamically — it makes for a great way to update an image on the fly. The GD Library allows you to apply your own watermarks to images dynamically too!