Counter.php
<?php
/*
optional query string parameters:
counterid: the counter to increment & display -- 1 or greater (default = 1)
fontnum: the font size used for displaying the number in the generated image -- 1, 2, 3, 4, or 5 (default = 4)
margin: the margin (in pixels) around the number in the generated image (default = 4 pixels)
*/
if (isset ($_GET['counterid']))
$counterid = $_GET['counterid'];
else
$counterid = 1;
@ $db = new mysqli ('localhost', 'username', 'password', 'database');
if (mysqli_connect_errno ())
exit;
$query = "update counters
set count = count + 1
where counterid = $counterid";
$result = $db->query ($query);
if (!$result)
exit;
$query = "select count
from counters
where counterid = $counterid";
$result = $db->query ($query);
if (!$result)
exit;
$row = $result->fetch_object ();
$myText = strval ($row->count);
if (isset ($_GET['fontnum']) && $_GET['fontnum'] >= 1 && $_GET['fontnum'] <= 5)
$fontNum = $_GET['fontnum'];
else
$fontNum = 4;
if (isset ($_GET['margin']))
$margin = $_GET['margin'];
else
$margin = 4;
$txtWidth = strlen ($myText) * imagefontwidth ($fontNum);
$txtHeight = imagefontheight ($fontNum);
$imgWidth = 2 * $margin + $txtWidth;
$imgHeight = 2 * $margin + $txtHeight;
$im = imagecreatetruecolor ($imgWidth, $imgHeight);
$bkColor = imagecolorallocate ($im, 239, 237, 222);
$txtColor = imagecolorallocate ($im, 0, 0, 153);
imagefill ($im, 0, 0, $bkColor);
$imgX = ($imgWidth - $txtWidth) / 2;
$imgY = ($imgHeight - $txtHeight) / 2;
imagestring ($im, $fontNum, $imgX, $imgY, $myText, $txtColor);
header ('Content-type: image/gif');
imagegif ($im);
imagedestroy ($im);
?>