2
// counter.php the program to show the counter number.
4
function CounterImage() {
5
$counterLogFile = ".counter.log";
7
if (file_exists($counterLogFile) == true) {
8
if(($fp= fopen($counterLogFile, "r+")) == false) {
9
printf("fopen of the file %s failed\n", $counterLogFile);
12
if (($content = fread($fp, filesize($counterLogFile))) == false) {
13
printf(" fread failed on the file %s\n", $counterLogFile);
16
$content=chop($content); //trim the return character
17
if (($imageLocation = convertToImage($content)) == false) {
18
printf("ConverttoImage failed\n");
22
if (rewind($fp) == 0) {
23
printf("rewind failed\n");
26
if (!fwrite($fp, $content, strlen($content))) {
27
printf("fwrite failed while updateing count in the file %s\n", $counterLogFile);
30
return $imageLocation;
34
if (($fp = fopen($counterLogFile, "w")) ==false) {
35
printf("fopen of the file %s failed\n", $counterLogFile);
38
if (!fwrite($fp, "1", 1)) {
39
printf("fwrite failed on the file %s\n", $counterLogFile);
46
function ConvertToImage($content) {
48
$imageFile = ".counter.png";
49
$relativePath = ".counter.png";
50
$noOfChars = strlen($content);
52
$charHeight = ImageFontHeight(5);
53
$charWidth = ImageFontWidth(5);
54
$strWidth = $charWidth * $noOfChars;
55
$strHeight = $charHeight;
58
$imgWidth = $strWidth + 15;
59
$imgHeight = $strHeight + 15;
60
$imgCenterX = $imgWidth /2;
61
$imgCenterY = $imgHeight /2;
63
$im = ImageCreate($imgWidth, $imgHeight);
64
$script = ImageColorAllocate($im, 0, 255, 0);
65
$outercolor = ImageColorAllocate($im, 99, 140, 214);
66
$innercolor = ImageColorAllocate($im, 0, 0, 0);
67
ImageFilledRectangle($im, 0, 0, $imgWidth, $imgHeight, $outercolor);
68
ImageFilledRectangle($im, 3, 3, $imgWidth -4, $imgHeight-4, $innercolor);
71
$drawPosX = $imgCenterX - ($strWidth /2) +1;
72
$drawPosY = $imgCenterY - ($strHeight /2);
73
ImageString($im, 5, $drawPosX, $drawPosY, $content, $script);
75
//save image and return
76
ImagePNG($im, $imageFile);