counter.php

00001 <?php
00002         // counter.php the program to show the counter number.
00003         
00004         function CounterImage() {
00005            $counterLogFile = ".counter.log";
00006            
00007            if (file_exists($counterLogFile) == true) {
00008               if(($fp= fopen($counterLogFile, "r+")) == false) {
00009                  printf("fopen of the file %s failed\n", $counterLogFile);
00010                  exit;
00011               }
00012               if (($content = fread($fp, filesize($counterLogFile))) == false) {
00013                  printf(" fread failed on the file %s\n", $counterLogFile);
00014                  exit;
00015               } else {
00016                  $content=chop($content); //trim the return character
00017                  if (($imageLocation = convertToImage($content)) == false) {
00018                     printf("ConverttoImage failed\n");
00019                     exit;
00020                  } else {
00021                     $content++;
00022                     if (rewind($fp) == 0) {
00023                        printf("rewind failed\n");
00024                        exit;
00025                     }
00026                     if (!fwrite($fp, $content, strlen($content))) {
00027                        printf("fwrite failed while updateing count in the file %s\n", $counterLogFile);
00028                        exit;
00029                     }
00030                     return $imageLocation;
00031                  }
00032               }
00033            }else {
00034               if (($fp = fopen($counterLogFile, "w")) ==false) {
00035                  printf("fopen of the file %s failed\n", $counterLogFile);
00036                  exit;
00037               }
00038               if (!fwrite($fp, "1", 1)) {
00039                  printf("fwrite failed on the file %s\n", $counterLogFile);
00040                  exit;
00041               }
00042            }
00043         }
00044 
00045 
00046         function ConvertToImage($content) {
00047            
00048            $imageFile = ".counter.png";
00049            $relativePath = ".counter.png";
00050            $noOfChars = strlen($content);
00051            
00052            $charHeight = ImageFontHeight(5);
00053            $charWidth = ImageFontWidth(5);
00054            $strWidth = $charWidth * $noOfChars;
00055            $strHeight = $charHeight;
00056            
00057            //15 padding
00058            $imgWidth = $strWidth + 15;
00059            $imgHeight = $strHeight + 15;
00060            $imgCenterX = $imgWidth /2;
00061            $imgCenterY = $imgHeight /2;
00062            
00063            $im = ImageCreate($imgWidth, $imgHeight);
00064            $script = ImageColorAllocate($im, 0, 255, 0);
00065            $outercolor = ImageColorAllocate($im, 99, 140, 214);
00066            $innercolor = ImageColorAllocate($im, 0, 0, 0);
00067            ImageFilledRectangle($im, 0, 0, $imgWidth, $imgHeight, $outercolor);
00068            ImageFilledRectangle($im, 3, 3, $imgWidth -4, $imgHeight-4, $innercolor);
00069            
00070            //draw string
00071            $drawPosX = $imgCenterX - ($strWidth /2) +1;
00072            $drawPosY = $imgCenterY - ($strHeight /2);
00073            ImageString($im, 5, $drawPosX, $drawPosY, $content, $script);
00074            
00075            //save image and return
00076            ImagePNG($im, $imageFile);
00077            return $relativePath;
00078         }
00079 ?>    

Generated on Wed Jul 26 13:30:39 2006 for XSB by  doxygen 1.4.5