note 87717 deleted from function.imagettftext by danbrown

From: Date: Thu, 18 Dec 2008 15:53:51 +0000
Subject: note 87717 deleted from function.imagettftext by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148320@lists.php.net to get a copy of this message
Note Submitter: conrad at wadds dot net ---- Hi! I had a requirement to have a hit counter without have an shtml (server parsed) page and used the example as a starting point. I have attempted some in-line documentation, but as they say, you get what you pay for. I hope that you find it useful. <?php // This script has been loosely adapted from the example in the PHP Manual // entry for the 'imagettftext' function, and the PHP Hit Counter // script by Klemen Stirn available from the http://www.phpjunkyard.com // website. // // It's purpose is to count unique visitors to a site and produce/display // a graphic of the result. // // To use this script in your web site, use the following example as a template: // <img src="counter.php?page=index"> // where the index variable can be the page name or any other counter. // The log file must exist or an error value of -1 will be produced. // // I have to admit to being neither a web designer nor a PHP guru // (most of my scripting has been done either in bash or perl) // so please forgive the possible errors or style gaffes. // // Feel free to use or modify this code as suits you requirements. // // Feedback to conrad at wadds dot net would be appreciated. // // Enjoy! // Set the content-type header('Content-type: image/png'); // If we are passed a page name, use that // or default to "index" $page = $_GET['page'] or $page = 'index'; // The log can reside in an absolute path, // or a path relative to the script location, // but MUST exist. $logfile = 'logs/' . $page . '.txt'; // Number of hours required to pass until // a visitor is considered "new" $unique_hours = 24; /* Does the log exist? */ if (file_exists($logfile)) { /* Get current count */ $count = trim(file_get_contents($logfile)) or $count = 0; // If the counter_unique cookie has expired, or was never set if ($_COOKIE['counter_unique']!=$page) { /* Increase the count by 1 */ $count = $count + 1; $fp = @fopen($logfile,'w+') or die('ERROR: Cannot write to the log file ('.$logfile.')'); flock($fp, LOCK_EX); fputs($fp, $count); flock($fp, LOCK_UN); fclose($fp); /* Print the Cookie and P3P compact privacy policy */ header('P3P: CP="NOI NID"'); setcookie('counter_unique', $page, time()+60*60*$unique_hours); } } else { // A simple type of error $count = -1; } // Replace path by your own font path $font = '/usr/share/fonts/arial.ttf'; // Create the image, size will be dependant on both text // being displayed and font being used. $im = imagecreatetruecolor(200, 30); // 200px x 30px // Create some colours $white = imagecolorallocate($im, 255, 255, 255); $black = imagecolorallocate($im, 0, 0, 0); // and fill the image with a colour imagefilledrectangle($im, 0, 0, 199, 29, $white); // The text to draw into the image $text = 'We have had ' . $count . ' visitors'; // Add the text to the image imagettftext($im, 12, 0, 10, 20, $black, $font, $text); // Using imagepng() results in clearer text compared with imagejpeg() imagepng($im); imagedestroy($im); ?>

« previous php.notes (#148320) next »