And this simple project would be very helpful for your programming projects.
apart automatically. Captcha is a program which helps to protect your website from
and determine the user is human or not. So, you need to add Captcha for protecting contact form, comment form and any other forms from spam. But many times you need to add a simple PHP captcha in form. This tutorial helps you to implement Captcha functionality using PHP in your website.
we have created a Captcha PHP class which provides you highly customization option for PHP captcha. Using Captchagenerator script you can easily add captcha to the form. In the main Captchagenerator class contains 2 functions createCaptcha() and hexToRgb(). hexToRgb() function is used by createCaptcha() function, so you should need to call only createCaptcha() function.
<?php
class Captchagenerator
{
var $word = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var $length = 5;
var $img_width = 160;
var $img_height = 50;
var $font_path = '';
var $font_size = 25;
var $expiration = 7200;
var $bg_color = '#ffffff';
var $border_color = '#996666';
var $text_color = '#cc9999';
var $grid_color = '#ffb6b6';
var $shadow_color = '#fff0f0';
public function __construct
($config = array()){ foreach ($config as $key => $val){
$method = 'set_'.$key;
$this->$method($val);
}else{
$this->$key = $val;
}
}
}
}
return FALSE;
}
}
public function createCaptcha(){
$str = '';
for ($i = 0; $i < $this->length; $i++){
}
$word = $str;
$angle = ($length >= 6) ?
rand(-($length-6), ($length-6)) : 0; $x_axis = rand(6, (360/$length)-16); $y_axis = ($angle >= 0 ) ?
rand($this->img_height, $this->img_width) : rand(6, $this->img_height);
}else{
$im = imagecreate($this->img_width, $this->img_height); }
$bgColorRgb = $this->hexToRgb($this->bg_color);
$borderColorRgb = $this->hexToRgb($this->border_color);
$textColorRgb = $this->hexToRgb($this->text_color);
$gridColorRgb = $this->hexToRgb($this->grid_color);
$shadowColorRgb = $this->hexToRgb($this->shadow_color);
$border_color = imagecolorallocate ($im, $borderColorRgb[0], $borderColorRgb[1], $borderColorRgb[2]); $text_color = imagecolorallocate ($im, $textColorRgb[0], $textColorRgb[1], $textColorRgb[2]); $grid_color = imagecolorallocate($im, $gridColorRgb[0], $gridColorRgb[1], $gridColorRgb[2]); $shadow_color = imagecolorallocate($im, $shadowColorRgb[0], $shadowColorRgb[1], $shadowColorRgb[2]);
$theta = 1;
$thetac = 7;
$radius = 16;
$circles = 20;
$points = 32;
for ($i = 0; $i < ($circles * $points) - 1; $i++){
$theta = $theta + $thetac;
$rad = $radius * ($i / $points );
$x = ($rad * cos($theta)) + $x_axis; $y = ($rad * sin($theta)) + $y_axis; $theta = $theta + $thetac;
$rad1 = $radius * (($i + 1) / $points);
$x1 = ($rad1 * cos($theta)) + $x_axis; $y1 = ($rad1 * sin($theta )) + $y_axis; imageline($im, $x, $y, $x1, $y1, $grid_color); $theta = $theta - $thetac;
}
$x = rand(0, $this->img_width/($length/1.5)); $y = $this->font_size+2;
for ($i = 0; $i < strlen($word); $i++) {
if ($use_font == FALSE){
$y = rand(0 , $this->img_height/2); $x += ($this->font_size);
}else{
$y = rand($this->img_height/2, $this->img_height-3); imagettftext($im, $this->font_size, $angle, $x, $y, $text_color, $this->font_path, substr($word, $i, 1)); $x += $this->font_size;
}
}
imagerectangle($im, 0, 0, $this->img_width-1, $this->img_height-1, $border_color); header('Content-Type: image/jpeg'); unset($_SESSION['captchaCode']); $_SESSION['captchaCode'] = $word;
}
public function hexToRgb($hex){
} else {
}
$rgb = array($r, $g, $b); return $rgb;
}
}
?>
this website.