Jump to content

PHP hit counter through image generator fault.


si9000

Recommended Posts

Hi,

 

I was wondering if anyone would be able to help me with a problem I have. I've made a small script to count page views, which is output through an image. The problem I am facing is that when the output number from the hit counter is passed through the image generator, it increases by 2 instead of 1. I have tested this with echos, die, etc etc and everything is fine its only when the number is output through the image generator that it adds 2 instead of 1.

 

here is the code, at the moment i just have it in 4 docs. first is the config.php:

 

<?php
//Enter your desired background image width below
define('IMAGE_WIDTH', 90);

//Enter your desired background image height below
define('IMAGE_HEIGHT', 60);

//Enter your desired background colour below
define('BACKGROUND_COLOUR_HEX', '#FFFFFF');

//Enter your desired text colour below
define('TEXT_COLOUR_HEX', '#FF0000');

//Enter your desired shadow colour below
define('SHADOW_COLOUR_HEX', '#C0C0C0');

//Enter a font size
define('FONT_SIZE', 23);

//Enter the X coordinates of the main text below
define('MAIN_X', 20);

//Enter the Y coordinates of the main text below
define('MAIN_Y', 23);

//Enter your desired font below
define('FONT_TYPE', 'arial.ttf');
?>

 

second lot of code is the main file, named page_counter.html but this doesnt matter.

 

<?php
include 'config.php';
/* -------------------------------------------------------------------------- */
//                       HEX TO RGB CONVERTER CODE
function hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }
   $rgb = array($r, $g, $b);
   
   return $rgb;
}

//hit counter code
$counter = file_get_contents("count.txt");
$counter = trim($counter);
$counter += 1;
file_put_contents("count.txt", $counter);

//hex to rgb colour converter code
$bg_rbg = hex2rgb(BACKGROUND_COLOUR_HEX);
$txt_rbg = hex2rgb(TEXT_COLOUR_HEX);
$shadow_rbg = hex2rgb(SHADOW_COLOUR_HEX);

/* -------------------------------------------------------------------------- */
//                       IMAGE GENERACTOR CODE

header('Content-Type: image/png');

//Creates the background
$im = imagecreatetruecolor(IMAGE_WIDTH, IMAGE_HEIGHT);

//Creates the colours
$background = imagecolorallocate($im, $bg_rbg[0], $bg_rbg[1], $bg_rbg[2]);
$main_text_colour = imagecolorallocate($im, $txt_rbg[0], $txt_rbg[1], $txt_rbg[2]);
$shadow_colour = imagecolorallocate($im, $shadow_rbg[0], $shadow_rbg[1], $shadow_rbg[2]);
imagefilledrectangle($im, 0, 0, 500, 100, $background);

//Assigns a font path and font
$font = dirname(__FILE__). '/' . FONT_TYPE;

//Adds some shadow to the text
imagettftext($im, FONT_SIZE, 0, MAIN_X+1, MAIN_Y+1, $shadow_colour, $font, $counter);

//Creates the image
imagettftext($im, FONT_SIZE, 0, MAIN_X, MAIN_Y, $main_text_colour, $font, $counter);

//Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

 

third file is count.txt with nothing in it, just the number you wish to start from so just say 0.

 

and the last file, is just a font file. in this case the font used is arial.ttf.

 

 

Thank you very much in advanced to any1 who has taken the time to look over the code and give some feedback.

 

Link to comment
Share on other sites

Somewhere you're requesting the file twice if it is incrementing by 2 instead of 1.

 

How are you calling the file that creates the image? Where/how are you displaying the image in page_counter.html? Nothing in your code I can see will cause it to increment by 2. I have tested it and it is incrementing fine.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.