Verification Code picture HTML code:
The first image authentication code class, the other 6 kinds of authentication code class please download view:
Class captcha{
/**
+----------------------------------------------------------
* Generate Verification Code
+----------------------------------------------------------
* @static
* @access Public
+----------------------------------------------------------
* @param int $len Verification Code character number
* @param int $font _size Verification code font size
* @param string $name session Name
* @param int $width picture length
* @param int $height picture height
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
static function Generate ($len =4, $font _size=48, $name = ' captcha ', $width = ', $height = ') {
if ($width = = ") $width = ($font _size+5) * ($len + 1);
if ($height = = ") $height = ($font _size) *2;
$chars = ' bcdefhkmnrstuvwxyABCDEFGHKMNPRSTUVWXY345689 ';
$str = ';
for ($i =0; $i < $len; $i + +) {
$str. = substr ($chars, Mt_rand (0,strlen ($chars)-1), 1);
}
$_session[$name]= $str;//write session
For ($num =0 $num <10; $num + +) {
Ob_start ();
$image =imagecreatetruecolor ($width, $height);//Create Picture
$BG _color=imagecolorallocate ($image, 255,255,255);//Set Background color
$border _color=imagecolorallocate ($image, 100,100,100);//Set Border color
$text _color=imagecolorallocate ($image, 0,0,0);//Set the authentication code color
Imagefilledrectangle ($image, 0,0, $width-1, $height-1, $BG _color);//Fill picture background color
Imagerectangle ($image, 0,0, $width-1, $height-1, $border _color);//Fill picture border color
For ($i =0 $i <5; $i + +) {
$line _color=imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));/Interference Line Color
Imageline ($image, rand (0, $width), 0, $width, $height, $line _color);/Draw a line segment
}
For ($i =0 $i <500; $i + +) {
$dot _color=imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));/Interference point color
Imagesetpixel ($image, rand ()% $width, rand ()% $height, $dot _color);/Draw a pixel.
}
for ($i =0; $i < $len; $i + +) {
Imagettftext ($image, $font _size,rand ( -3,3), $font _size/2+ ($font _size+5) * $i, $height/1.25-rand (2,3), $text _color, ' Groupsex.ttf ', $str [$i]);//write text to image in specified font
}
Imagegif ($image);
Imagedestroy ($image);
$imagedata [] = Ob_get_contents ();
Ob_clean ();
}
Require (' GIFEncoder.class.php ');
$gif = new Gifencoder ($imagedata);
Ob_clean ()//To prevent the "image because its own error can not be displayed" problem
Header (' content-type:image/gif ');
echo $gif->getanimation ();
}
}
Call Example
Session_Start ();
Captcha::generate (6,48);