Before I created the gas log on my site, I was using Green Hybrid to log gas usage/mileage. And since that time, they’ve had a graphic that you could add to your site, or in my case, forum signature that shows your mileage.
But, since I created my own log, I stopped updating Green Hybrid, so the data was out of date. I’ve long wanted to do something like this myself, so I went about creating a basic one last week.
It doesn’t look the best, but it works, and it updates based on the info in my DB. The image code is pretty simple, using the PHP GD functions, and it’s pretty quickly generated. If I ever got a LOT of traffic, I’d probably have the code write the image out to disk and regenerate it every so often, but for now, it’s generated on the fly.
Here’s sanitized code:
$im = @imagecreatetruecolor(350, 50)
or die("Cannot Initialize new GD image stream");
$background = imagecolorallocate($im, 255, 255, 255);
$im2 = @imagecreatetruecolor(352, 52);
$border = imagecolorallocate($im2, 44, 74, 7);
imagefill($im, 0, 0, $background);
imagefill($im2, 0, 0, $border);
imagecopymerge($im2, $im, 1, 1, 0, 0, 350, 50, 100);
$im = $im2;
$src = imagecreatefrompng('gingko.png');
imagecopymerge($im, $src, 1, 1, 0, 0, 48, 48, 100);
$green = imagecolorallocate($im, 88, 138, 21);
$black = imagecolorallocate($im, 0, 0, 0);
$mileage = round($row,1);
$miles = round($row) . " Miles Driven";
imagestring($im, 2, 55, 5, "Everybody Staze", $black);
imagestring($im, 2, 55, 25, "http://www.staze.org", $black);
imagestring($im, 4, 225, 5, "I get MPG", $black);
imagestring($im, 5, 271, 5, $mileage, $green);
imagestring($im, 2, 225, 25, $miles, $black);
Obviously the $mileage and $miles variables are populated with a DB query ($row and $row), but I’ve removed that for security reasons. GD is pretty easy to use. I might update the code to use a TTF font, but we’ll see. I don’t think it looks terrible, just a bit… plain. I thought I needed FreeType to do anything real fancy with fonts, but it seems GD MIGHT be able to (if you convert the ttf font to a gdf). We’ll see.