Session_Start (); /* * File:CaptchaSecurityImages.php * Author:simon Jarvis * copyright:2006 Simon Jarvis * date:03/08/06 * updated:07/02/07 * requirements:php 4/5 with GD and FreeType libraries * link:http://www.white-hat-web-design.co.uk/articles/php-captcha.php * * This program was free software; You can redistribute it and/or * Modify it under the terms of the GNU general public License * As published by the Free software Foundation; Either version 2 * of the License, or (at your option) any later version. * * This program was distributed in the hope that it'll be useful, * but without any WARRANTY; Without even the implied warranty of * merchantability or FITNESS for A particular PURPOSE. See the * GNU general public License-more details: * http://www.gnu.org/licenses/gpl.html * */ Class Captchasecurityimages { var $font = ' Monofont.ttf '; function Generatecode ($characters) { /* List all possible characters, similar looking characters and vowels has been removed */ $possible = ' 23456789bcdfghjkmnpqrstvwxyz '; $code = "; $i = 0; while ($i < $characters) { $code. = substr ($possible, Mt_rand (0, strlen ($possible)-1), 1); $i + +; } return $code; } function captchasecurityimages ($width = ' + ', $height = ' + ', $characters = ' 6 ') { $code = $this->generatecode ($characters); /* Font size would be 75% of the image Height */ $font _size = $height * 0.75; $image = @imagecreate ($width, $height) or Die (' cannot initialize new GD image stream '); /* Set the colours */ $background _color = imagecolorallocate ($image, 255, 255, 255); $text _color = Imagecolorallocate ($image, 20, 40, 100); $noise _color = imagecolorallocate ($image, 100, 120, 180); /* generate random dots in background */ for ($i =0; $i < ($width * $height)/3; $i + +) { Imagefilledellipse ($image, Mt_rand (0, $width), Mt_rand (0, $height), 1, 1, $noise _color); } /* Generate random lines in background */ for ($i =0; $i < ($width * $height)/150; $i + +) { Imageline ($image, Mt_rand (0, $width), Mt_rand (0, $height), Mt_rand (0, $width), Mt_rand (0, $height), $noise _color); } /* Create textbox and add text */ $textbox = Imagettfbbox ($font _size, 0, $this->font, $code) or Die (' Error in Imagettfbbox function '); $x = ($width-$textbox [4])/2; $y = ($height-$textbox [5])/2; Imagettftext ($image, $font _size, 0, $x, $y, $text _color, $this->font, $code) or Die (' Error in Imagettftext function '); /* Output CAPTCHA image to Browser */ Header (' Content-type:image/jpeg '); Imagejpeg ($image); Imagedestroy ($image); $_session[' security_code '] = $code; } } $width = Isset ($_get[' width ')? $_get[' width ': ' 120 '; $height = isset ($_get[' height ')? $_get[' height ']: ' 40 '; $characters = Isset ($_get[' characters ') && $_get[' characters '] > 1? $_get[' characters ': ' 6 '; $captcha = new Captchasecurityimages ($width, $height, $characters); ?> |