August 26, 201115 yr Heres a simple to use captcha class i've been working on,its not 100% done once it is i'll be releasing it as a zip file including font files with it and a captcha_show.php file that will make it easy to include in your page, but just thought i'd get some feedback on the class itself , you have have a look at the captcha in action here,http://www.submitbannerusa.com/add_banner.php, its shown at the bottom of the page. <?php class FireStorm_Captcha{ /** * Width of image *@Var int */ public $image_width; /** * Height of image *@Var int */ public $image_height; /** * Type of image to generate *@Var string */ public $image_type; /** *Length of code to generate *@var int */ public $code_len; /** *The ttf file for our font *@Var string */ public $ttf_file; /** *Background color for our image *@Var string */ public $background_color; /** *Text color for our image *@Var string */ public $text_color; /** *Line color for our image *@Var string */ public $line_color; /** *Circle color for our image *@Var string */ public $circle_color; /** *Pixel color for our image *@Var string */ public $pixel_color; /** *Choose to use lines on the image or not *@Var bool */ public $UseLines; /** *Choose to use circles on the image or not *@Var bool */ public $UseCircles; /** *Choose to use pixels on the image or not *@Var bool */ public $UsePixels; /** *Number of pixels to use on captcha *@Var int */ public $PixelNumber; /** *Number of circles to use on captcha *@Var int */ public $CircleNumber; /** *Number of lines to use on captcha *@Var int */ public $LineNumber; /** *Set line thickness for image *@Var int */ public $line_thickness; /** *Add shadow to image *@Var bool */ public $use_shadow; /** *Color for our shadow *@Var mixed */ public $shadow_color; /** *Character set for the captcha code *@Var string */ public $charset; /** *Font Size *@Var string */ public $FontSize; /** *Holds our code *@Var mixed */ private $FScode; /** *Gd image resource *@Access private *@Var resource */ private $firestorm_captcha; /** * FireStorm_Captcha::__construct() * * @return void */ public function __construct(){ if(session_id() == ''){ session_start(); } /** *Set up default values */ $this->image_width = 350; $this->image_height = 110; $this->code_len = 6; $this->FontSize = 56; $this->line_thickness = 1; $this->LineNumber = 10; $this->CircleNumber = 10; $this->PixelNumber = 1000; $this->UseLines = false; $this->UseCircles = false; $this->UsePixels = true; $this->use_shadow = true; $this->ttf_file = "fonts/Acidic.TTF"; $this->image_type = "image/jpeg"; $this->charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789"; } /** * FireStorm_Captcha::FS_save() * Save our code into a session * @return void */ public function FS_save(){ $_SESSION['firestorm_captcha'] = $this->FScode; } /** * FireStorm_Captcha::FS_CreateCode() * Create our code then save it to our session * @return void */ public function FS_CreateCode(){ $this->FScode = $this->FS_CodeGen($this->code_len);//Create code $this->FS_save();//Save code to session } /** * FireStorm_Captcha::FS_CodeGen() * Generate a captcha code from our charset * @param mixed $len * @return */ public function FS_CodeGen( $len ){ $FS_code = ''; for($i = 1,$clen = strlen($this->charset); $i <= $len; ++$i){ $FS_code .= $this->charset{rand(0,$clen-1)}; } return $FS_code; } /** * FireStorm_Captcha::FS_GetCode() * Get our code for displaying * @return */ public function FS_GetCode(){ if(isset($_SESSION['firestorm_captcha']) && !empty($_SESSION['firestorm_captcha'])){ return $_SESSION['firestorm_captcha']; } } /** * FireStorm_Captcha::FS_CreateNoise() * Create background Noise for our image * @return void */ public function FS_CreateNoise(){ if($this->UsePixels === true){ for($i = 1; $i <= $this->PixelNumber; $i++){ imagesetpixel($this->firestorm_captcha,rand(0,$this->image_width),rand(0,$this->image_height),$this->pixel_color); } } if($this->UseCircles === true){ for($i = 1; $i <= $this->CircleNumber; $i++){ imageellipse($this->firestorm_captcha,rand(0,$this->image_width),rand(0,$this->image_height),rand(10,30),rand(10,30),$this->circle_color); } } if($this->UseLines === true){ imagesetthickness($this->firestorm_captcha,$this->line_thickness); for($i = 1; $i <= $this->LineNumber; $i++){ imageline($this->firestorm_captcha,rand(0,$this->image_width),rand(0,$this->image_height),rand(0,$this->image_width),rand(0,$this->image_height),$this->line_color); } } } /** * FireStorm_Captcha::FS_Logo() * Logo for firestorm captcha * @return void */ public function FS_Logo(){ imagettftext($this->firestorm_captcha,10, 0, 30, 10,$this->text_color,$this->ttf_file,"FireStorm Captcha(Created By Rizo Development)"); } /** * FireStorm_Captcha::FS_DoImage() * Create our image * @return void */ public function FS_DoImage(){ $this->firestorm_captcha = imagecreate($this->image_width,$this->image_height); $this->background_color = imagecolorallocate($this->firestorm_captcha,255,255,255); $this->text_color = imagecolorallocate($this->firestorm_captcha,142,35,35); $this->line_color = imagecolorallocate($this->firestorm_captcha,000,000,000); $this->circle_color = imagecolorallocate($this->firestorm_captcha,rand(0,255),rand(0,255),rand(0,255)); $this->pixel_color = imagecolorallocate($this->firestorm_captcha,000,000,000); $this->shadow_color = imagecolorallocate($this->firestorm_captcha,138,138,138); $this->FS_CreateCode(); /** *Add shadow */ if($this->use_shadow === true){ imagettftext($this->firestorm_captcha,$this->FontSize, 0, 14, 85,$this->shadow_color,$this->ttf_file,$this->FScode); } /** *Write to our image */ imagettftext($this->firestorm_captcha,$this->FontSize, 0, 11, 75,$this->text_color,$this->ttf_file,$this->FScode); /***********DO LOGO**********************/ $this->FS_Logo();//DO NOT REMOVE THIS LINE /****Create background noise on captcha*****/ $this->FS_CreateNoise(); /** *Output image */ $this->FS_Output(); } /** * FireStorm_Captcha::FS_Output() * Output our finished image * @return void */ public function FS_Output(){ /** *Switch through our image types for output */ switch($this->image_type){ case 'image/jpeg': header("Content-Type: image/jpeg"); imagejpeg($this->firestorm_captcha); break; case 'image/gif': header("Content-Type: image/gif"); imagegif($this->firestorm_captcha); break; case 'image/png': header("Content-Type: image/png"); imagepng($this->firestorm_captcha); break; } imagedestroy($this->firestorm_captcha); exit; } } ?>
August 26, 201115 yr Heres a simple to use captcha class i've been working on,its not 100% done once it is i'll be releasing it as a zip file including font files with it and a captcha_show.php file that will make it easy to include in your page, but just thought i'd get some feedback on the class itself , you have have a look at the captcha in action here,http://www.submitbannerusa.com/add_banner.php, its shown at the bottom of the page. <?php class FireStorm_Captcha{ /** * Width of image *@Var int */ public $image_width; /** * Height of image *@Var int */ public $image_height; /** * Type of image to generate *@Var string */ public $image_type; /** *Length of code to generate *@var int */ public $code_len; /** *The ttf file for our font *@Var string */ public $ttf_file; /** *Background color for our image *@Var string */ public $background_color; /** *Text color for our image *@Var string */ public $text_color; /** *Line color for our image *@Var string */ public $line_color; /** *Circle color for our image *@Var string */ public $circle_color; /** *Pixel color for our image *@Var string */ public $pixel_color; /** *Choose to use lines on the image or not *@Var bool */ public $UseLines; /** *Choose to use circles on the image or not *@Var bool */ public $UseCircles; /** *Choose to use pixels on the image or not *@Var bool */ public $UsePixels; /** *Number of pixels to use on captcha *@Var int */ public $PixelNumber; /** *Number of circles to use on captcha *@Var int */ public $CircleNumber; /** *Number of lines to use on captcha *@Var int */ public $LineNumber; /** *Set line thickness for image *@Var int */ public $line_thickness; /** *Add shadow to image *@Var bool */ public $use_shadow; /** *Color for our shadow *@Var mixed */ public $shadow_color; /** *Character set for the captcha code *@Var string */ public $charset; /** *Font Size *@Var string */ public $FontSize; /** *Holds our code *@Var mixed */ private $FScode; /** *Gd image resource *@Access private *@Var resource */ private $firestorm_captcha; /** * FireStorm_Captcha::__construct() * * @return void */ public function __construct(){ if(session_id() == ''){ session_start(); } /** *Set up default values */ $this->image_width = 350; $this->image_height = 110; $this->code_len = 6; $this->FontSize = 56; $this->line_thickness = 1; $this->LineNumber = 10; $this->CircleNumber = 10; $this->PixelNumber = 1000; $this->UseLines = false; $this->UseCircles = false; $this->UsePixels = true; $this->use_shadow = true; $this->ttf_file = "fonts/Acidic.TTF"; $this->image_type = "image/jpeg"; $this->charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789"; } /** * FireStorm_Captcha::FS_save() * Save our code into a session * @return void */ public function FS_save(){ $_SESSION['firestorm_captcha'] = $this->FScode; } /** * FireStorm_Captcha::FS_CreateCode() * Create our code then save it to our session * @return void */ public function FS_CreateCode(){ $this->FScode = $this->FS_CodeGen($this->code_len);//Create code $this->FS_save();//Save code to session } /** * FireStorm_Captcha::FS_CodeGen() * Generate a captcha code from our charset * @param mixed $len * @return */ public function FS_CodeGen( $len ){ $FS_code = ''; for($i = 1,$clen = strlen($this->charset); $i <= $len; ++$i){ $FS_code .= $this->charset{rand(0,$clen-1)}; } return $FS_code; } /** * FireStorm_Captcha::FS_GetCode() * Get our code for displaying * @return */ public function FS_GetCode(){ if(isset($_SESSION['firestorm_captcha']) && !empty($_SESSION['firestorm_captcha'])){ return $_SESSION['firestorm_captcha']; } } /** * FireStorm_Captcha::FS_CreateNoise() * Create background Noise for our image * @return void */ public function FS_CreateNoise(){ if($this->UsePixels === true){ for($i = 1; $i <= $this->PixelNumber; $i++){ imagesetpixel($this->firestorm_captcha,rand(0,$this->image_width),rand(0,$this->image_height),$this->pixel_color); } } if($this->UseCircles === true){ for($i = 1; $i <= $this->CircleNumber; $i++){ imageellipse($this->firestorm_captcha,rand(0,$this->image_width),rand(0,$this->image_height),rand(10,30),rand(10,30),$this->circle_color); } } if($this->UseLines === true){ imagesetthickness($this->firestorm_captcha,$this->line_thickness); for($i = 1; $i <= $this->LineNumber; $i++){ imageline($this->firestorm_captcha,rand(0,$this->image_width),rand(0,$this->image_height),rand(0,$this->image_width),rand(0,$this->image_height),$this->line_color); } } } /** * FireStorm_Captcha::FS_Logo() * Logo for firestorm captcha * @return void */ public function FS_Logo(){ imagettftext($this->firestorm_captcha,10, 0, 30, 10,$this->text_color,$this->ttf_file,"FireStorm Captcha(Created By Rizo Development)"); } /** * FireStorm_Captcha::FS_DoImage() * Create our image * @return void */ public function FS_DoImage(){ $this->firestorm_captcha = imagecreate($this->image_width,$this->image_height); $this->background_color = imagecolorallocate($this->firestorm_captcha,255,255,255); $this->text_color = imagecolorallocate($this->firestorm_captcha,142,35,35); $this->line_color = imagecolorallocate($this->firestorm_captcha,000,000,000); $this->circle_color = imagecolorallocate($this->firestorm_captcha,rand(0,255),rand(0,255),rand(0,255)); $this->pixel_color = imagecolorallocate($this->firestorm_captcha,000,000,000); $this->shadow_color = imagecolorallocate($this->firestorm_captcha,138,138,138); $this->FS_CreateCode(); /** *Add shadow */ if($this->use_shadow === true){ imagettftext($this->firestorm_captcha,$this->FontSize, 0, 14, 85,$this->shadow_color,$this->ttf_file,$this->FScode); } /** *Write to our image */ imagettftext($this->firestorm_captcha,$this->FontSize, 0, 11, 75,$this->text_color,$this->ttf_file,$this->FScode); /***********DO LOGO**********************/ $this->FS_Logo();//DO NOT REMOVE THIS LINE /****Create background noise on captcha*****/ $this->FS_CreateNoise(); /** *Output image */ $this->FS_Output(); } /** * FireStorm_Captcha::FS_Output() * Output our finished image * @return void */ public function FS_Output(){ /** *Switch through our image types for output */ switch($this->image_type){ case 'image/jpeg': header("Content-Type: image/jpeg"); imagejpeg($this->firestorm_captcha); break; case 'image/gif': header("Content-Type: image/gif"); imagegif($this->firestorm_captcha); break; case 'image/png': header("Content-Type: image/png"); imagepng($this->firestorm_captcha); break; } imagedestroy($this->firestorm_captcha); exit; } } ?> think you may wanna rephrase the word simple in your title
August 28, 201115 yr Author Heres the full zip file attached to this post, to include the captcha image all you have to do is <img src="FireStorm_Captcha/firestorm_captcha_show.php" alt="" /> then to do a check to see if the code is entered correctly you can do. if($_SESSION['firestorm_captcha'] !== $_POST['security_field']){ //error out } you may also open up firestorm_captcha_show.php to change some settings this is the contents of that file <?php require_once('./firestorm.php'); /** *Create captcha object */ $FS_captcha = new FireStorm_Captcha; /** *Edit below captcha settings if need be */ //$FS_captcha->image_width = 390; //$FS_captcha->image_height = 120; //$FS_captcha->code_len = 6; //$FS_captcha->FontSize = 56; //$FS_captcha->line_thickness = 1; //$FS_captcha->LineNumber = 10; //$FS_captcha->CircleNumber = 10; //$FS_captcha->PixelNumber = 2500; //$FS_captcha->UseLines = false; //$FS_captcha->UseCircles = false; //$FS_captcha->UsePixels = true; //$FS_captcha->use_shadow = true; //$FS_captcha->shadow_depth = 89; //$FS_captcha->Sig = ""; //$FS_captcha->ttf_file = "fonts/Kyboshed.ttf"; //$FS_captcha->image_type = "image/jpeg"; //$FS_captcha->charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789"; /** *Display captcha image */ $FS_captcha->FS_DoImage(); ?> over all its a pretty easy class to use and will have updates to it, cheers. FireStorm_Captcha.zip
August 28, 201115 yr Author You could easily extends that a little to include a ran dom font. E.g. set an array of fonts and for the ->ttf_filevalue set this to choose a random key from your fonts array. Personally, I also wouldn't have the following to display the image: <img src="FireStorm_Captcha/firestorm_captcha_show.php" alt="" /> as I think it's just another place for someone to start messing. May be worth using htaccess tom rewite it to soemthing like /captcha-2176714587234.jpg , where the number could be some hash of their session_id, to make the image harder to manipulate for the "casual user who likes to mess about". i actually did throw in a commented out random font generator after i posted the above code little outdated heres the updated code <?php /** *HELPFUL NOTES BELOW FOR MANAGING YOUR CAPTCHA */ /** *SETTING UP RANDOM LINES,CIRCLES,OR PIXEL NUMBERS *************************************************** *You can randomize line numbers,circle numbers or pixel numbers as well, *to randomize the number of lines. do this with the variable LineNumber *$this->LineNumber = rand(3,20); by adding rand(3,20) you can make it display random lines * using random line numbers between 3 and 20, these numbers can be changed to suit you *instead of using a solid number like 10 */ /** *EDITING THE CHARSET ************************************* *To do number only charsets you can edit the variable *$this->charset to *$this->charset = "0123456789"; *Or to do only capital letters you can do *$this->charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; * and do the same for lowercase if you just want lower case letters */ class FireStorm_Captcha{ /** * Width of image *@Var int */ public $image_width; /** * Height of image *@Var int */ public $image_height; /** * Type of image to generate *@Var string */ public $image_type; /** *Length of code to generate *@var int */ public $code_len; /** *The ttf file for our font *@Var string */ public $ttf_file; /** *Background color for our image *@Var string */ public $background_color; /** *Text color for our image *@Var string */ public $text_color; /** *Line color for our image *@Var string */ public $line_color; /** *Circle color for our image *@Var string */ public $circle_color; /** *Pixel color for our image *@Var string */ public $pixel_color; /** *Choose to use lines on the image or not *@Var bool */ public $UseLines; /** *Choose to use circles on the image or not *@Var bool */ public $UseCircles; /** *Choose to use pixels on the image or not *@Var bool */ public $UsePixels; /** *Number of pixels to use on captcha *@Var int */ public $PixelNumber; /** *Number of circles to use on captcha *@Var int */ public $CircleNumber; /** *Number of lines to use on captcha *@Var int */ public $LineNumber; /** *Set line thickness for image *@Var int */ public $line_thickness; /** *Add shadow to image *@Var bool */ public $use_shadow; /** *Depth of shadow *@Var int */ public $shadow_depth; /** *Color for our shadow *@Var mixed */ public $shadow_color; /** *Character set for the captcha code *@Var string */ public $charset; /** *Font Size *@Var string */ public $FontSize; /** *Holds users sig *@Var string */ public $Sig; /** *Holds our code *@Var mixed */ private $FScode; /** *Gd image resource *@Access private *@Var resource */ private $firestorm_captcha; /** * FireStorm_Captcha::__construct() * * @return void */ public function __construct(){ if(session_id() == ''){ session_start(); } /** *FIRESTORM_CAPTCHA SETTINGS **************************************** *Edit the below settings to customize your captcha image */ $this->image_width = 390; $this->image_height = 120; $this->code_len = 6; $this->FontSize = 56; $this->line_thickness = 1; $this->LineNumber = 10; $this->CircleNumber = 10; $this->PixelNumber = 2500; $this->UseLines = false; $this->UseCircles = false; $this->UsePixels = true; $this->use_shadow = true; $this->shadow_depth = 89; $this->Sig = ""; $this->ttf_file = "fonts/Kyboshed.ttf"; $this->image_type = "image/jpeg"; $this->charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789"; /** *Random Font File *This works but is commented out for simplicity of the script, *this should only be used by advance users,uncomment the below *code to generate random font styles on your image */ /** $RandFont = rand(0,3); switch($RandFont){ case 0: $this->ttf_file = "fonts/Kyboshed.ttf"; break; case 1: $this->ttf_file = "fonts/Acidic.TTF"; break; case 2: $this->ttf_file = "fonts/BLOODY.TTF"; break; case 3: $this->ttf_file = "fonts/AlphaWood.ttf"; break; } **/ } /** * FireStorm_Captcha::SetSig() * User can set a signiture , which can be there url etc... * @return void */ public function SetSig(){ if($this->Sig !== ''){ imagettftext($this->firestorm_captcha,12, 0, 110, 110,$this->text_color,$this->ttf_file,$this->Sig); } } /** * FireStorm_Captcha::FS_save() * Save our code into a session * @return void */ public function FS_save(){ $_SESSION['firestorm_captcha'] = strtolower($this->FScode); } /** * FireStorm_Captcha::FS_CreateCode() * Create our code then save it to our session * @return void */ public function FS_CreateCode(){ $this->FScode = strtolower($this->FS_CodeGen($this->code_len));//Create code $this->FS_save();//Save code to session } /** * FireStorm_Captcha::FS_CodeGen() * Generate a captcha code from our charset * @param mixed $len * @return */ public function FS_CodeGen( $len ){ $FS_code = ''; for($i = 1,$clen = strlen($this->charset); $i <= $len; ++$i){ $FS_code .= $this->charset{rand(0,$clen-1)}; } return $FS_code; } /** * FireStorm_Captcha::FS_GetCode() * Get our code for displaying * @return */ public function FS_GetCode(){ if(isset($_SESSION['firestorm_captcha']) && !empty($_SESSION['firestorm_captcha'])){ return $_SESSION['firestorm_captcha']; } } /** * FireStorm_Captcha::FS_CreateNoise() * Create background Noise for our image * @return void */ public function FS_CreateNoise(){ if($this->UsePixels === true){ for($i = 1; $i <= $this->PixelNumber; $i++){ imagesetpixel($this->firestorm_captcha,rand(0,$this->image_width),rand(0,$this->image_height),$this->pixel_color); } } if($this->UseCircles === true){ for($i = 1; $i <= $this->CircleNumber; $i++){ imageellipse($this->firestorm_captcha,rand(0,$this->image_width),rand(0,$this->image_height),rand(10,30),rand(10,30),$this->circle_color); } } if($this->UseLines === true){ imagesetthickness($this->firestorm_captcha,$this->line_thickness); for($i = 1; $i <= $this->LineNumber; $i++){ imageline($this->firestorm_captcha,rand(0,$this->image_width),rand(0,$this->image_height),rand(0,$this->image_width),rand(0,$this->image_height),$this->line_color); } } } /** * FireStorm_Captcha::FS_Logo() * Logo for firestorm captcha * @return void */ public function FS_Logo(){ imagettftext($this->firestorm_captcha,10, 0, 30, 10,$this->text_color,$this->ttf_file,"FireStorm Captcha(Created By Rizo Development)"); } /** * FireStorm_Captcha::FS_DoImage() * Create our image * @return void */ public function FS_DoImage(){ $this->firestorm_captcha = imagecreate($this->image_width,$this->image_height); $this->background_color = imagecolorallocate($this->firestorm_captcha,255,255,255); $this->text_color = imagecolorallocate($this->firestorm_captcha,142,35,35); $this->line_color = imagecolorallocate($this->firestorm_captcha,000,000,000); $this->circle_color = imagecolorallocate($this->firestorm_captcha,rand(0,255),rand(0,255),rand(0,255)); $this->pixel_color = imagecolorallocate($this->firestorm_captcha,000,000,000); $this->shadow_color = imagecolorallocate($this->firestorm_captcha,138,138,138); $this->FS_CreateCode(); /** *Add shadow */ if($this->use_shadow === true){ imagettftext($this->firestorm_captcha,$this->FontSize, 0, 14, $this->shadow_depth,$this->shadow_color,$this->ttf_file,$this->FScode); } /** *Write to our image */ imagettftext($this->firestorm_captcha,$this->FontSize, 0, 11, 75,$this->text_color,$this->ttf_file,$this->FScode); /***********DO LOGO**********************/ $this->FS_Logo();//DO NOT REMOVE THIS LINE /***************Captcha Sig**************************/ $this->SetSig(); /****Create background noise on captcha*****/ $this->FS_CreateNoise(); /** *Output image */ $this->FS_Output(); } /** * FireStorm_Captcha::FS_Output() * Output our finished image * @return void */ public function FS_Output(){ /** *Switch through our image types for output */ switch($this->image_type){ case 'image/jpeg': header("Content-Type: image/jpeg"); imagejpeg($this->firestorm_captcha); break; case 'image/gif': header("Content-Type: image/gif"); imagegif($this->firestorm_captcha); break; case 'image/png': header("Content-Type: image/png"); imagepng($this->firestorm_captcha); break; } imagedestroy($this->firestorm_captcha); exit; } } ?> mmm and could i have a .htaccess example of what your talking bout please? not my strongest point
August 29, 201115 yr Author Now has an option for multi-letter colors and tilting of letters, plus different sizes for letters, i have not released this update yet still working on it but thought i'd show a screenshot anybody who maybe wants to work on this project as well give me a pm
August 30, 201115 yr Author Not tested, but should work, assuming you;re just genrating numbers in your filename: RewriteBase /FireStorm_Captcha RewriteRule ^captcha-([0-9]+)\.jpg$ firestorm_captcha_show.php [L] Or, if's say a certain hash you're using, you could change the regex to a slightly more compliacted one, that only matches letters/numbers of a certain length. Thanks for the suggestion, i am deff gonna try this out in my spare time thanks again +1
Create an account or sign in to comment