<?php
$conf["file"]        = "counter.dat";   //filename for storing

$conf["length"]      = 6;               //digits in counter

$conf["imageheight"] = 50;              //height
$conf["automaticwidth"] = TRUE;
$conf["imagewidth"]  = 144;             //width

$conf["bgcolor"]     = "000000";        //background color (hex)
$conf["fgcolor"]     = "FFFFFF";        //foreground color (hex)

$conf["textalign"]   = "center";        //left, center or right

$conf["TTFfilename"] = "./arialbd.ttf"; //TTF file (hint: C:\Windows\Fonts)
$conf["TTFsize"]     = 12;              //size in points
$conf["TTFangle"]    = 0;               //angle of text

/**************************************/



clearstatcache();
if(file_exists($conf["file"]) && is_readable($conf["file"]))
{
  $array = file($conf["file"]);
  $count = $array["0"];
}

$count++; 

$fp = fopen($conf["file"], "w");
fwrite($fp, $count);
fclose($fp);


$text = str_repeat("0", $conf["length"]);

$text = substr_replace($text, $count, -(strlen($count)));
$text = substr($text, -($conf["length"]));

$size = ImageTTFBBox($conf["TTFsize"], $conf["angle"], $conf["TTFfilename"], $text);
$textwidth = ($size["2"] - $size["0"]);
$textheight = ($size["5"] - $size["3"]);

$conf["imagewidth"] = ($conf["automaticwidth"] == TRUE) ? ($textwidth + 6) : $conf["imagewidth"];


switch($conf["textalign"])
{
  case "left":
    $textleft    = 0;
  break;
  case "center":
    $textleft    = (($conf["imagewidth"]  - $textwidth) / 2);
  break;
  case "right":
    $textleft    = ($conf["imagewidth"]  - $textwidth);
  break;
}

$textbottom  = (($conf["imageheight"] - $textheight) / 2);

$img = ImageCreate($conf["imagewidth"], $conf["imageheight"]);

$bgred   = hexdec(substr($conf["bgcolor"], 0, 2));
$bggreen = hexdec(substr($conf["bgcolor"], 2, 2));
$bgblue  = hexdec(substr($conf["bgcolor"], 4, 2));
$bgcolor = ImageColorAllocate($img, $bgred, $bggreen, $bgblue);

$fgred   = hexdec(substr($conf["fgcolor"], 0, 2));
$fggreen = hexdec(substr($conf["fgcolor"], 2, 2));
$fgblue  = hexdec(substr($conf["fgcolor"], 4, 2));
$fgcolor = ImageColorAllocate($img, $fgred, $fggreen, $fgblue);

ImageFilledRectangle($img, 0, 0, $conf["imagewidth"], $conf["imageheight"], $bgcolor);

ImageTTFText($img, $conf["TTFsize"], $conf["TTFangle"], $textleft, $textbottom, $fgcolor, $conf["TTFfilename"], $text);

header('Content-type: image/png');
imagepng($img);

ImageDestroy($img);
?>