/    Sign up×
Community /Pin to ProfileBookmark

Simple Captcha Class

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,[URL=”http://www.submitbannerusa.com/add_banner.php”]http://www.submitbannerusa.com/add_banner.php[/URL] shown at the bottom of the page.

[code=php]
<?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;
/**
*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 = rand(3,7);
$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 = 80;
$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 = $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;
}

}

?>
[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @webdeveloper93 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...