临时弄了一个图片验证码,不过那条线没有处理的很好,位置固定的,应该跟随字符变化的,求改进呀~~~ 还有就是最好写成类的方式,不用过程的。。。 验证的时候只需要对比$_SESSI
还有就是最好写成类的方式,不用过程的。。。
验证的时候只需要对比$_SESSION['VerifyCode']就可以了。。。
//===================================================================//
如果不放session里,可以生成一个token作为验证码的查询key,再把对应key的vlaue存放在mysql或者memcache里,在表单里传递token过来后,查询验证。。。
//==================================================================//
最下面提供了一个较完整的verifyCode.class.php的验证码类,是几年前写的了,可以看下下。。。。。。
1. [代码][PHP]代码
<?php session_start(); //=====================================================================// $width = 120; $height = 50; $point = 200; $size = 25; $line = 9; $offset = 10; $font = 'Consolab.ttf'; $code = mt_rand(1111, 9999); $_SESSION['VerifyCode'] = md5($code); //=====================================================================// $img = imagecreate($width, $height); $back = imagecolorallocate($img, 255, 220, 230); $color = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); $list = str_split(strval($code)); foreach($list as $key => $value){ $angle = mt_rand(-60, 60); imagettftext($img, $size, $angle, $key*$size+$offset, $key+$size+$offset, $color, $font, $value); } for($i = 1; $i < $point; $i ++){ imagesetpixel($img, rand()%$width, rand()%$height, $color); } for($j = 1; $j < $line; $j ++){ imageline($img, $line-$j, $size-$j, $width-$line-$j, $height-$line-$j, $color); } header('Content-type: image/png'); print imagepng($img);
2. [图片] QQ截图20150206164721.png
3. [代码][PHP]代码
<?php header("Cache-Control:no-cache"); header("Pragma:no-cache"); session_start(); class verifyCode{ private $img; private $code; private $imgBg; private $randColor; private $codeArray; private $fontAngle; private $fontText; private $imgType="png"; // "gif" "jpeg" "png" "wbmp" private $imgWidth=120; //图像宽度 private $imgHeight=40; //图像高度 private $fontSize=22; //字体大小 private $fontNum=5; // 验证码字体(字符)个数 private $fontType="Arial.ttf"; // 字体类型 private $fontOutType=2; // 1 表示全为数字验证码(需要进行加密判断), 2 表示数字与字母混合型验证码 (需要进行加密判断) private $pointNum=250; //噪声点个数 private $pointX=120; //一般设置为与图像宽度相同 private $pointY=50; //一般设置为与图像高度相同 private $lineNum=10; //噪声线条个数 private $lineX1=array(0,1); //噪声线X轴范围 private $lineY1=array(0,50); //噪声线Y轴范围 private $lineX2=array(100,150); private $lineY2=array(0,50); private $xx; //字符写入图像时的基量 private $xxx=20; //字符写入图像时的增量 private $yy; private $yyy=6; function __construct(){ if(! function_exists("gd_info")){ die("gd was not found!"); } } private function setImg(){ $this->img=imagecreate($this->imgWidth,$this->imgHeight); $this->imgBg=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255)); $this->randColor=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255)); self::writeImg(); return $this->img; } private function randCode(){ if($this->fontOutType==1){ for($l=0;$l<$this->fontNum;$l++){ $this->code.=substr(strval(mt_rand()),0,1); } $_SESSION["code"]=md5($this->code); } if($this->fontOutType==2){ $this->codeArray=str_split("0aAb1BcC2dDe3EfF4gGh5HiI6jJk7KlL8mMn9NoO8pPq7QrR6sSt5TuU4vVw3WxX2yYz1Z0"); for($h=0;$h<$this->fontNum;$h++){ $this->code.=strval($this->codeArray[rand(0,70)]); } $_SESSION["code"]=md5($this->code); } return $this->code; } private function writeText(){ $this->fontAngle=rand(-25,25); $this->fontText=str_split(self::randCode()); $this->xx=intval($this->imgWidth/$this->fontNum)-$this->xxx/2; $this->yy=intval($this->imgHeight/2); for($m=0;$m<$this->fontNum;$m++){$this->randColor=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255)); imagettftext($this->img,$this->fontSize,$this->fontAngle,$this->xx+$this->xxx*$m,$this->yy+$this->yyy,$this->randColor,$this->fontType, $this->fontText[$m]); } } private function writeImg(){ for($i=0;$i<$this->pointNum;$i++){ imagesetpixel($this->img,rand()%$this->pointX,rand()%$this->pointY,$this->randColor); } for($j=0;$j<$this->lineNum;$j++){ imageline($this->img,rand($this->lineX1[0],$this->lineX1[1]),rand($this->lineY1[0],$this->lineY1[1]),rand($this->lineX2[0],$this->lineX2[1]),rand($this->lineY2[0],$this->lineY2[1]),$this->randColor); } self::writeText(); } private function getImgType($imgType){ $this->imgType=strtolower($imgType); switch($this->imgType){ case "gif": return "Content-type:image/gif"; break; case "jpeg": return "Content-type:image/jpeg"; break; case "png": return "Content-type:image/png"; break; case "wbmp": return "Content-type:image/wbmp"; break; default: die("This type was unknow!"); } } public function getImg(){ header(self::getImgType($this->imgType)); $this->imgType=strtolower($this->imgType); switch($this->imgType){ case "gif": imagegif(self::setImg()); imagedestroy(self::setImg()); break; case "jpeg": imagejpeg(self::setImg()); imagedestroy(self::setImg()); break; case "png": imagepng(self::setImg()); imagedestroy(self::setImg()); break; case "wbmp": imagewbmp(self::setImg()); imagedestroy(self::setImg()); break; default: die("This type was unknow!"); } } } $obj=new verifyCode(); $obj->getImg();