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'] >= && $_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 $margin $txtWidth;
   
$imgHeight $margin $txtHeight;

   
$im imagecreatetruecolor ($imgWidth$imgHeight);

   
$bkColor imagecolorallocate ($im239237222);
   
$txtColor imagecolorallocate ($im00153);

   
imagefill ($im00$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);
?>