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 is 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 is distributed in the hope that it will be useful, * But without any warranty; without even the implied warranty * MERCHANTABILITY or fitness for a special PURPOSE. See * GNU General Public License for 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 have 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 = '000000', $ height = '40', $ characters = '6 '){ $ Code = $ this-> generateCode ($ characters ); /* Font size will 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']: '20140901 '; $ Height = isset ($ _ GET ['height'])? $ _ GET ['height']: '40 '; $ Characters = isset ($ _ GET ['characters ']) & $ _ GET ['characters']> 1? $ _ GET ['characters ']: '6 '; $ Captcha = new CaptchaSecurityImages ($ width, $ height, $ characters ); ?> |