使用php怎么生成一个复杂的验证码(php,开发技术)

时间:2024-05-10 10:19:34 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

php有什么用

php是一个嵌套的缩写名称,指的是英文超级文本预处理语言(php:Hypertext Preprocessor)的缩写,它的语法混合了C、Java、Perl以及php自创新的语法,主要用来做网站开发,许多小型网站都用php开发,因为php是开源的,从而使得php经久不衰。

1、字体变型 (一般通过算法,进行扭曲)

使用php怎么生成一个复杂的验证码

2、字体黏贴 (这里面以qq验证码为代表了,目前网上还是很难找到,破解qq验证码的)

使用php怎么生成一个复杂的验证码

3、干扰线,噪点 (这种识别起来相当容易,很容易被程序自动化识别)

对于上面提到,第1,2 二种方法,在识别时候,是比较困难的。

具体实现代码:

<?php/***带文字旋转,倾斜,黏贴,加正弦干扰线验证码**/classUtils_Caption{ var$Width=60;//图片宽 var$Height=30;//图片高 var$Length=4;//验证码位数 var$BgColor="#FFFFFF";//背景色 var$TFonts=array("font.ttf"); var$TFontSize=array(17,20);//字体大小范围 var$TFontAngle=array(-20,20);//旋转角度 var$Chars="0123456789";//验证码范围(字母数字) var$Code=array();//验证码 var$Image="";//图形对象 var$FontColors=array('#f36161','#6bc146','#5368bd');//字体颜色,红绿蓝 var$TPadden=0.75;///字符间距,多少个字符 var$Txbase=5;///x轴两边距离 var$Tybase=5;///y轴两边距离 var$TLine=true;///画干扰线 publicfunctionRandRSI()///生成验证码 { $this->TFontAngle=range($this->TFontAngle[0],$this->TFontAngle[1]); $this->TFontSize=range($this->TFontSize[0],$this->TFontSize[1]); $arr=array(); $Chars=$this->Chars; $TFontAngle=$this->TFontAngle; $TFontSize=$this->TFontSize; $FontColors=$this->FontColors; $code=""; $font=dirname(__FILE__)."/font/".$this->TFonts[0]; $charlen=strlen($Chars)-1; $anglelen=count($TFontAngle)-1;//角度范围 $fontsizelen=count($TFontSize)-1;//角度范围 $fontcolorlen=count($FontColors)-1;//角度范围 for($i=0;$i<$this->Length;$i++)///得到字符与颜色 { $char=$Chars[rand(0,$charlen)];///得到字符 $angle=$TFontAngle[rand(0,$anglelen)];///旋转角度 $fontsize=$TFontSize[rand(0,$fontsizelen)];///字体大小 $fontcolor=$FontColors[rand(0,$fontcolorlen)];///字体大小 $bound=$this->_calculateTextBox($fontsize,$angle,$font,$char);///得到范围 $arr[]=array($fontsize,$angle,$fontcolor,$char,$font,$bound);///得到矩形框 $code.=$char; } $this->Code=$arr;//验证码 return$code; } publicfunctionDraw()///画图 { if(empty($this->Code))$this->RandRSI(); $codes=$this->Code;///用户验证码 $wh=$this->_getImageWH($codes); $width=$wh[0]; $height=$wh[1];///高度 $this->Width=$width; $this->Height=$height; $this->Image=imageCreate($width,$height); $image=$this->Image; $back=$this->_getColor2($this->_getColor($this->BgColor));///背景颜色 imageFilledRectangle($image,0,0,$width,$height,$back);///填充背景 $TPadden=$this->TPadden; $basex=$this->Txbase; $color=null; foreach($codesas$v)///逐个画字符 { $bound=$v[5]; $color=$this->_getColor2($this->_getColor($v[2])); imagettftext($image,$v[0],$v[1],$basex,$bound['height'],$color,$v[4],$v[3]); $basex=$basex+$bound['width']*$TPadden-$bound['left'];///计算下一个左边距 } $this->TLine?$this->_wirteSinLine($color,$basex):null;///画干扰线 header("Content-type:image/png"); imagepng($image); imagedestroy($image); } /** *通过字体角度得到字体矩形宽度* * *@paramint$font_size字体尺寸 *@paramfloat$font_angle旋转角度 *@paramstring$font_file字体文件路径 *@paramstring$text写入字符 *@returnarray返回长宽高 */ privatefunction_calculateTextBox($font_size,$font_angle,$font_file,$text){ $box=imagettfbbox($font_size,$font_angle,$font_file,$text); $min_x=min(array($box[0],$box[2],$box[4],$box[6])); $max_x=max(array($box[0],$box[2],$box[4],$box[6])); $min_y=min(array($box[1],$box[3],$box[5],$box[7])); $max_y=max(array($box[1],$box[3],$box[5],$box[7])); returnarray( 'left'=>($min_x>=-1)?-abs($min_x+1):abs($min_x+2), 'top'=>abs($min_y), 'width'=>$max_x-$min_x, 'height'=>$max_y-$min_y, 'box'=>$box ); } privatefunction_getColor($color)//#ffffff { returnarray(hexdec($color[1].$color[2]),hexdec($color[3].$color[4]),hexdec($color[5].$color[6])); } privatefunction_getColor2($color)//#ffffff { returnimagecolorallocate($this->Image,$color[0],$color[1],$color[2]); } privatefunction_getImageWH($data) { $TPadden=$this->TPadden; $w=$this->Txbase; $h=0; foreach($dataas$v) { $w=$w+$v[5]['width']*$TPadden-$v[5]['left']; $h=$h>$v[5]['height']?$h:$v[5]['height']; } returnarray(max($w,$this->Width),max($h,$this->Height)); } //画正弦干扰线 privatefunction_wirteSinLine($color,$w) { $img=$this->Image; $h=$this->Height; $h2=rand(-5,5); $h3=rand(-1,1); $w2=rand(10,15); $h4=rand(4,6); for($i=-$w/2;$i<$w/2;$i=$i+0.1) { $y=$h/$h4*sin($i/$w2)+$h/2+$h2; imagesetpixel($img,$i+$w/2,$y,$color); $h3!=0?imagesetpixel($img,$i+$w/2,$y+$h3,$color):null; } }}

DEMO代码:

$rsi=newUtils_Caption();$rsi->TFontSize=array(15,17);$rsi->Width=50;$rsi->Height=25;$code=$rsi->RandRSI();session_start();$_SESSION["CHECKCODE"]=$code;$rsi->Draw();
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:使用php怎么生成一个复杂的验证码的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:使用php怎么取出数组的单个值下一篇:

7 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18