效果:
代码:
<?php /* 一个简单的验证码类,网上凑来的三款验证码, 我只是把他改成了类的形式,觉得还不错,分享一下。 注意:session的值默认是md5加密的! */ class Verify{ private $type;//验证码类型(1:数字,2:算数,3:字母数字) private $width;//图片宽度 private $height;//图片长度 private $length; //验证码长度(算数类的话就是代表随机最大的数) private $session; //session名称(默认为verify) function __construct($type=1,$width=60,$height=24,$length=4,$session='verify'){ $this->type = intval($type); $this->width = intval($width); $this->height = intval($height); $this->length = intval($length); $this->showVerify($this->type,$this->width,$this->height,$this->length,$session); } //输出验证码类型 function showVerify($type,$width,$height,$length,$session){ switch($type){ case 1: $this->getNum($width,$height,$length,$session); break; case 2: $this->getCode($width,$height,$length,$session); break; case 3: $this->getMath($width,$height,$length,$session); break; default:$this->getNum($width,$height,$length,$session); } } //数字 static function getNum($width,$height,$langth,$session) { $code = ""; for ($i = 0; $i < $langth; $i++) { $code .= rand(0, 9); } //4位验证码也可以用rand(1000,9999)直接生成 //将生成的验证码写入session,备验证页面使用 $_SESSION[$session] = md5($code); //创建图片,定义颜色值 //Header("Content-type: image/PNG"); $im = imagecreate($width, $height); $black = imagecolorallocate($im, 0, 0, 0); $gray = imagecolorallocate($im, 200, 200, 200); $bgcolor = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $gray); //画边框 imagerectangle($im, 0, 0, $width-1, $height-1, $black); //随机绘制两条虚线,起干扰作用 $style = array ( $black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray ); imagesetstyle($im, $style); $y1 = rand(0, $height); $y2 = rand(0, $height); $y3 = rand(0, $height); $y4 = rand(0, $height); imageline($im, 0, $y1, $width, $y3, IMG_COLOR_STYLED); imageline($im, 0, $y2, $width, $y4, IMG_COLOR_STYLED); //在画布上随机生成大量黑点,起干扰作用; for ($i = 0; $i < 80; $i++) { imagesetpixel($im, rand(0, $width), rand(0, $height), $black); } //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 $strx = rand(3, 8); for ($i = 0; $i < $langth; $i++) { $strpos = rand(1, 6); imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black); $strx += rand(8, 12); } Verify::output($im); } //字母和数字 static function getCode($width,$height,$langth,$session) { // 去掉了 0 1 O l 等 //$bigchar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';//想要有大写字母可以取消注释 $str = '23456789abcdefghijkmnpqrstuvwxyz'.$bigchar; $code = ''; for ($i = 0; $i < $langth; $i++) { $code .= $str[mt_rand(0, strlen($str)-1)]; } //将生成的验证码写入session,备验证页面使用 $_SESSION[$session] = md5($code); //创建图片,定义颜色值 //Header("Content-type: image/PNG"); $im = imagecreate($width, $height); $black = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); $gray = imagecolorallocate($im, 118, 151, 199); $bgcolor = imagecolorallocate($im, 235, 236, 237); //画背景 imagefilledrectangle($im, 0, 0, $width, $height, $bgcolor); //画边框 imagerectangle($im, 0, 0, $width-1, $height-1, $gray); //imagefill($im, 0, 0, $bgcolor); //在画布上随机生成大量点,起干扰作用; for ($i = 0; $i < 80; $i++) { imagesetpixel($im, rand(0, $width), rand(0, $height), $black); } //将字符随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 $strx = rand(3, 8); for ($i = 0; $i < $langth; $i++) { $strpos = rand(1, 6); imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black); $strx += rand(8, 14); } Verify::output($im); } //算数 static function getMath($width,$height,$langth,$session) { if($langth < 20 ) $langth = 20;//最小20,太简单没意思,嘿嘿 $im = imagecreate($width, $height); //imagecolorallocate($im, 14, 114, 180); // background color $red = imagecolorallocate($im, 255, 0, 0); $widthhite = imagecolorallocate($im, 255, 255, 255); $num1 = rand(1, $langth); $num2 = rand(1, $langth); $_SESSION[$session] = md5($num1 + $num2); $gray = imagecolorallocate($im, 118, 151, 199); $black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100)); //画背景 imagefilledrectangle($im, 0, 0, $width, $height, $black); //在画布上随机生成大量点,起干扰作用; for ($i = 0; $i < 80; $i++) { imagesetpixel($im, rand(0, $width), rand(0, $height), $gray); } imagestring($im, 5, 5, 4, $num1, $red); imagestring($im, 5, 30, 3, "+", $red); imagestring($im, 5, 45, 4, $num2, $red); imagestring($im, 5, 70, 3, "=", $red); imagestring($im, 5, 80, 2, "?", $widthhite); Verify::output($im); } //输出图片 static function output($im, $type='png') { header("Content-type: image/" . $type); imagepng($im); imagedestroy($im); } } ?>