/** * verification code class for generating various verification codes */class captcha{ /** * Generate verification Code * @param int $length code character length * @param int $width width * @param int $height Length * @param string $type type * @return */ public Static function getcode ($length =4, $width =50, $height =22, $type = ' png ') { //session_start (); $randval = self::randstring ($length); //$_ session[' captcha ']= $randval; $width = ($length *9+10) > $width? $length *9+10: $width; if ( $type! = ' gif ' && function_exists (' Imagecreatetruecolor ')) { $im = @imagecreatetruecolor ($width, $height); }else { $im = @imagecreate ($width, $height); } $r = array (225,255,255,223); $g = array (225,236,237,255); $b = array (225,236,166,125); $key = mt_rand (0,3); $backColor = Imagecolorallocate ($im, $r [$key], $g [$kEY], $b [$key]); //background color (random) $borderColor = imagecolorallocate ($im, 100, 100, 100); //Border Color $pointColor = imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand ( 0,255)) //point color @imagefilledrectangle ($im, 0, 0, $width - 1, $height - 1, $backColor); @imagerectangle ($im, 0, 0, $width -1, $height -1, $borderColor); $ Stringcolor = imagecolorallocate ($im, Mt_rand (0,200), Mt_rand (0,120), Mt_rand (0,120)); // interference for ($i =0; $i <10; $i + +) { $fontcolor =imagecolorallocate ($im, Mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); imagearc ($im, Mt_rand ( -10, $width), Mt_rand ( -10, $height), Mt_rand (30,300), Mt_rand (20,200), 55,44, $fontcolor); } for ($i =0; $i <25; $i + +) { $fontcolor =imagecolorallocate ($im, Mt_rand ( 0,255), Mt_rand (0,255), Mt_rand (0,255)); Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pointColor); } @imagestring ($im, 10, 20, 10, $ randval, $stringColor); header ("content-type: image/". $type ); $ImageFun = ' Image '. $type; $ImageFun ($im), imagedestroy ($im); return $randval; }}
The above is the complete method to generate a picture verification code, the following is the use of this method to generate a verification code:
Include ". /lib/captcha.class.php "; $captcha = new Captcha (); $checkCode = $captcha->getcode (4,75,32); Echo $checkCode;
Finally, the effect of the page display is this, refresh one at a time:
PHP Generate Image Verification code