si9000 Posted October 1, 2013 Share Posted October 1, 2013 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 belowdefine('IMAGE_WIDTH', 90);//Enter your desired background image height belowdefine('IMAGE_HEIGHT', 60);//Enter your desired background colour belowdefine('BACKGROUND_COLOUR_HEX', '#FFFFFF');//Enter your desired text colour belowdefine('TEXT_COLOUR_HEX', '#FF0000');//Enter your desired shadow colour belowdefine('SHADOW_COLOUR_HEX', '#C0C0C0');//Enter a font sizedefine('FONT_SIZE', 23);//Enter the X coordinates of the main text belowdefine('MAIN_X', 20);//Enter the Y coordinates of the main text belowdefine('MAIN_Y', 23);//Enter your desired font belowdefine('FONT_TYPE', 'arial.ttf');?> second lot of code is the main file, named page_counter.html but this doesnt matter. <?phpinclude 'config.php';/* -------------------------------------------------------------------------- */// HEX TO RGB CONVERTER CODEfunction 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 CODEheader('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 textimagettftext($im, FONT_SIZE, 0, MAIN_X+1, MAIN_Y+1, $shadow_colour, $font, $counter);//Creates the imageimagettftext($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. Quote Link to comment https://forums.phpfreaks.com/topic/282606-php-hit-counter-through-image-generator-fault/ Share on other sites More sharing options...
Ch0cu3r Posted October 1, 2013 Share Posted October 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282606-php-hit-counter-through-image-generator-fault/#findComment-1452053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.