Send Captcha
First things first. I want to say thanks to Vic Fieger, Magnus Cederholm, Koczman Bálint (Cpr.Sparhelt) and Bitstream Inc. for develop the fonts used in this project.
true,
'randCaptchaLeng'=>true,
'randUppercase'=>true,
'randPattern'=>true,
'randColor'=>true,
'randAngle'=>true);
var $font=array('./VeraBd.ttf','./FFF_Tusj.ttf');
private function Captcha(){
$_SESSION["captcha_code"]='';
$random_alpha = md5(rand());
$leng=6;
if($this->captchaParams['randCaptchaLeng']){ $leng=rand(6,8);}
$fontSize=20;
$captcha_code = substr($random_alpha, 0, $leng);
$imagelenght=$leng*$fontSize+10;
$imageheight=$fontSize+24;
$baseLineText=$imageheight-10;
$target_layer = imagecreatetruecolor($imagelenght,$imageheight);
if($this->captchaParams['randColor']){
$captcha_background = imagecolorallocate($target_layer, 255, rand(0,255), 119);
} else {
$captcha_background = imagecolorallocate($target_layer, 255, 168, 119);
}
imagefill($target_layer,0,0,$captcha_background);
$captcha_text_color = imagecolorallocate($target_layer, 0, 0, 0);
$grey = imagecolorallocate($target_layer, 128, 128, 128);
$functions[0]=function ($code){return $code;};
$functions[1]=function ($code){return strtoupper($code);};
if($this->captchaParams['randPattern']){
for ($j=0;$j<10;$j++){
$x1=rand(0,$imagelenght);
$y1=rand(0,$imageheight);
$x2=rand(0,$imagelenght);
$y2=rand(0,$imageheight);
$x3=rand(0,$imagelenght);
$y3=rand(0,$imageheight);
$points=array($x1,$y1,$x2,$y2,$x3,$y3);
imagepolygon($target_layer,$points,3,$captcha_text_color);
}
}
for($i=0;$icaptchaParams['randUppercase']){
$code=$functions[rand(0,1)]($captcha_code[$i]);
}
$_SESSION["captcha_code"].=$code;
$angl=array(0,15,-15);
$ang=0;
if($this->captchaParams['randAngle']){$ang=$angl[rand(0,2)];}
// Adding shadow...
$sizeOftheFont=$fontSize;
if($this->captchaParams['randFontSizes']){ $sizeOftheFont=rand(10,$fontSize+5);}
$font=$this->font[rand(0,count($this->font))];
imagettftext($target_layer,$sizeOftheFont,$ang,7+$i*$fontSize,$baseLineText+2,$grey,$font,$code);
//Addingtext...
imagettftext($target_layer,$sizeOftheFont,$ang,5+$i*$fontSize,$baseLineText,$captcha_text_color,
$font,$code);
}
header("Content-type: image/jpeg");
imagejpeg($target_layer);
}
private function setHtml(){
return '
'.$this->setJS();
}
private function setJS(){
return "
";
}
function __construct(){
$this->url='http://'.$_SERVER['SERVER_NAME'].str_replace($_SERVER['DOCUMENT_ROOT'], '',__FILE__);
if($_SERVER['HTTPS']){
$this->url='https://'.$_SERVER['SERVER_NAME'].str_replace($_SERVER['DOCUMENT_ROOT'], '',__FILE__);
}
if (isset($_GET['getHtml'])){
echo $this->setHtml();
} else {
$this->Captcha();
}
}
}
$captcha = new ezcaptcha();
?>
HTML code: Client Side
This modul use JQuery, you have to add JQuery in your HTML code.
Insert the html tags where you want to show the captcha
Javascript code: Client Side Insert this javascript at the end of your html file to load the captcha component
(function(){
$.ajax({url:"/class_ezcaptcha.php?getHtml=1", success: function(result){
$("captcha").html(result);
}});
})();