[GuangzhouPHP]Simplepackageverificationcode.代码:[PHP]纯文本查看复制代码?010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596_w=$w;$this->_h=$h;$this->_img=imagecreatetruecolor($w,$h);//Fillthebackgroundcolor$color=$this->getColor();//CallthegetColormethodtoobtainarandomcolorimagefill($this->_img,0,0,$color);}#AssignThemethodofcolorprivatefunctiongetColor($r='',$g='',$b=''){//If$risanemptystring,itmeansthatnoparametersarepassed,thenwewillgivearandomvalue;ifIfitisnotallequaltoanemptystring,itmeansthattheuserhaspassedthespecifiedcolorvaluewhencalling,thenjustusethecolorvaluepassedbytheuserdirectly.$r=($r==='')?mt_rand(0,255):$r;$g=($g==='')?mt_rand(0,255):$g;$b=($b==='')?mt_rand(0,255):$b;//allocatecolorreturnimagecolorallocate($this->_img,$r,$g,$b);}#BuildrandomcharactersandwritetocanvaspublicfunctionwriteStr($num=4){$strArr=array_merge(range('a','z'),range('A','Z'),range(0,9));//构建随机字体//生成指定数量的随机字符串$str='';for($i=0;$i<$num;$i++){$key=mt_rand(0,count($strArr)-1);$str.=$strArr[$key];}//在画布上写$x=$this->_w/5;//文字起点的x坐标$y=$this->_h*2/3;//文字起点的y坐标textstartingpoint$color=$this->getColor();//分配一个随机颜色$size=$this->_w*3/20;//字体的大小是宽度的3/20imagettftext($this->_img,$size,0,$x,$y,$color,'H:/project/public/fonts/font1.ttf',$str);return$str;}#设置干扰点publicfunctionsetPoint($num=300){for($i=0;$i<$num;$i++){$bx=mt_rand(0,$this->_w);//起点的x坐标$by=mt_rand(0,$this->_h);//起点的y坐标$ex=mt_rand($bx-2,$bx+2);//终点的x坐标$ey=mt_rand($by-2,$by+2);//终点的y坐标$color=$this->getColor();//分配随机颜色imageline($this->_img,$bx,$by,$ex,$ey,$color);}}#设置干扰线publicfunctionsetLine($num=10){for($i=0;$i<$num;$i++){$bx=mt_rand(0,$this->_w/2);//起点的x坐标$by=mt_rand(0,$this->_h);//起点的y坐标$ex=mt_rand($this->_w/2,$this->_w);//终点的x坐标$ey=mt_rand(0,$this->_h);//终点的y坐标$color=$this->getColor();//分配随机颜色imageline($this->_img,$bx,$通过,$ex,$ey,$color);}}#输出图片到浏览器publicfunctionoutput(){header('Content-type:image/jpeg');imagejpeg($this->_img);}}$obj=newCaptchaTool;$obj->writeStr();$obj->output();影响:
