php 验证码【倾斜,正弦干扰线,黏贴,旋转】

 好久没有写帖子了。一直忙着新的项目。 最近,做验证码程序,一直想做一个简洁大方,自动识别比较困难的。 通过这些时候整理搜集,发现一般做法有以下几种方案:

1、字体变型 (一般通过算法,进行扭曲,比较有代表性就是:http://code.google.com/p/cool-php-captcha/ 

image

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

image

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

 

对于上面提到,第1,2 二种方法,在识别时候,是比较困难的。个人比较喜欢第二种方法,感觉看起来不是很费力。而扭曲的文字,总觉得怪怪的。 哈哈,纯粹个人喜好了。

实现代码:

PHP代码
  1. <?php  
  2. /** 
  3.  *带文字旋转,倾斜,黏贴,加正弦干扰线验证码* 
  4.  *@version  0.1 
  5.  *@author http://www.cnblogs.com/chengmo 
  6.  *@copyright 程默 QQ:8292669 
  7.  */  
  8. class Utils_Caption  
  9. {  
  10.     var $Width      = 60;           //图片宽  
  11.     var $Height     = 30;           //图片高  
  12.     var $Length     = 4;            //验证码位数  
  13.     var $BgColor    = "#FFFFFF";    //背景色  
  14.   
  15.     var $TFonts = array("font.ttf");  
  16.     var $TFontSize=array(17,20); //字体大小范围  
  17.     var $TFontAngle=array(-20,20); //旋转角度  
  18.   
  19.     var $Chars   = "0123456789";         //验证码范围(字母数字)  
  20.   
  21.     var $Code    = array();              //验证码  
  22.     var $Image   = "";              //图形对象  
  23.     var $FontColors=array('#f36161','#6bc146','#5368bd');  //字体颜色,红绿蓝  
  24.     var $TPadden = 0.75;///字符间距,多少个字符  
  25.     var $Txbase = 5;///x轴两边距离  
  26.     var $Tybase =5 ;///y轴两边距离  
  27.     var $TLine =true; ///画干扰线  
  28.   
  29.   
  30.     public  function RandRSI() ///生成验证码  
  31.     {  
  32.         $this->TFontAngle=range($this->TFontAngle[0],$this->TFontAngle[1]);  
  33.         $this->TFontSize=range($this->TFontSize[0],$this->TFontSize[1]);  
  34.   
  35.         $arr=array();  
  36.         $Chars=$this->Chars;  
  37.         $TFontAngle=$this->TFontAngle;  
  38.         $TFontSize=$this->TFontSize;  
  39.         $FontColors=$this->FontColors;  
  40.         $code="";  
  41.         $font=dirname(__FILE__)."/font/".$this->TFonts[0];  
  42.   
  43.         $charlen=strlen($Chars)-1;  
  44.         $anglelen=count($TFontAngle)-1; // 角度范围  
  45.         $fontsizelen=count($TFontSize)-1; // 角度范围  
  46.         $fontcolorlen=count($FontColors)-1; // 角度范围  
  47.   
  48.         for($i=0;$i<$this->Length;$i++) ///得到字符与颜色  
  49.         {  
  50.             $char=$Chars[rand(0,$charlen)]; ///得到字符  
  51.             $angle=$TFontAngle[rand(0,$anglelen)]; ///旋转角度  
  52.             $fontsize=$TFontSize[rand(0,$fontsizelen)]; ///字体大小  
  53.             $fontcolor=$FontColors[rand(0,$fontcolorlen)]; ///字体大小  
  54.   
  55.             $bound=$this->_calculateTextBox($fontsize,$angle,$font,$char); ///得到范围  
  56.   
  57.             $arr[]=array($fontsize,$angle,$fontcolor,$char,$font,$bound);  ///得到矩形框  
  58.             $code.=$char;  
  59.         }  
  60.         $this->Code=$arr//验证码  
  61.         return $code;  
  62.     }  
  63.   
  64.     public function Draw() ///画图  
  65.     {  
  66.         if(emptyempty($this->Code)) $this->RandRSI();  
  67.         $codes=$this->Code; ///用户验证码  
  68.   
  69.   
  70.         $wh=$this->_getImageWH($codes);  
  71.   
  72.         $width=$wh[0];  
  73.         $height=$wh[1]; ///高度  
  74.   
  75.         $this->Width=$width;  
  76.         $this->Height=$height;  
  77.   
  78.         $this->Image = imageCreate( $width$height );  
  79.         $image=$this->Image;  
  80.   
  81.         $back = $this->_getColor2($this->_getColor( $this->BgColor)); ///背景颜色  
  82.         imageFilledRectangle($image, 0, 0, $width$height$back); ///填充背景  
  83.   
  84.         $TPadden=$this->TPadden;  
  85.   
  86.         $basex=$this->Txbase;  
  87.         $color=null;  
  88.         foreach ($codes as $v///逐个画字符  
  89.         {  
  90.             $bound=$v[5];  
  91.             $color=$this->_getColor2($this->_getColor($v[2]));  
  92.             imagettftext($image$v[0], $v[1], $basex$bound['height'],$color , $v[4], $v[3]);  
  93.             $basex=$basex+$bound['width']*$TPadden-$bound['left'];///计算下一个左边距  
  94.         }  
  95.         $this->TLine?$this->_wirteSinLine($color,$basex):null; ///画干扰线  
  96.         header("Content-type: image/png");  
  97.         imagepng( $image);  
  98.         imagedestroy($image);  
  99.   
  100.     }  
  101.   
  102.     /** 
  103.      *通过字体角度得到字体矩形宽度* 
  104.      * 
  105.      * @param int $font_size 字体尺寸 
  106.      * @param float $font_angle 旋转角度 
  107.      * @param string $font_file 字体文件路径 
  108.      * @param string $text 写入字符 
  109.      * @return array 返回长宽高 
  110.      */  
  111.     private function _calculateTextBox($font_size$font_angle$font_file$text) {  
  112.         $box = imagettfbbox($font_size$font_angle$font_file$text);  
  113.   
  114.         $min_x = min(array($box[0], $box[2], $box[4], $box[6]));  
  115.         $max_x = max(array($box[0], $box[2], $box[4], $box[6]));  
  116.         $min_y = min(array($box[1], $box[3], $box[5], $box[7]));  
  117.         $max_y = max(array($box[1], $box[3], $box[5], $box[7]));  
  118.   
  119.         return array(  
  120.         'left' => ($min_x >= -1) ? -abs($min_x + 1) : abs($min_x + 2),  
  121.         'top' => abs($min_y),  
  122.         'width' => $max_x - $min_x,  
  123.         'height' => $max_y - $min_y,  
  124.         'box' => $box  
  125.         );  
  126.     }  
  127.   
  128.     private function  _getColor( $color ) //#ffffff  
  129.     {  
  130.         return array(hexdec($color[1].$color[2]),hexdec($color[3].$color[4]),hexdec($color[5].$color[6]));  
  131.     }  
  132.   
  133.     private function  _getColor2( $color ) //#ffffff  
  134.     {  
  135.         return imagecolorallocate ($this->Image, $color[0], $color[1], $color[2]);  
  136.     }  
  137.   
  138.     private function _getImageWH($data)  
  139.     {  
  140.         $TPadden=$this->TPadden;  
  141.         $w=$this->Txbase;  
  142.         $h=0;  
  143.         foreach ($data as $v)  
  144.         {  
  145.             $w=$w+$v[5]['width']*$TPadden-$v[5]['left'];  
  146.             $h=$h>$v[5]['height']?$h:$v[5]['height'];  
  147.         }  
  148.         return array(max($w,$this->Width),max($h,$this->Height));  
  149.     }  
  150.   
  151.     //画正弦干扰线  
  152.     private function _wirteSinLine($color,$w)  
  153.     {  
  154.         $img=$this->Image;  
  155.   
  156.         $h=$this->Height;  
  157.         $h1=rand(-5,5);  
  158.         $h2=rand(-1,1);  
  159.         $w2=rand(10,15);  
  160.         $h3=rand(4,6);  
  161.   
  162.         for($i=-$w/2;$i<$w/2;$i=$i+0.1)  
  163.         {  
  164.             $y=$h/$h3*sin($i/$w2)+$h/2+$h1;  
  165.             imagesetpixel($img,$i+$w/2,$y,$color);  
  166.             $h2!=0?imagesetpixel($img,$i+$w/2,$y+$h2,$color):null;  
  167.         }  
  168.     }  
  169. }  

 

主要特点是:旋转,然后黏贴,干扰线是线粗细可以变,然后正弦波形可以变化。

比较复杂是:calculateTextBox 这个函数,这个是得到字符旋转后的宽度高度。

使用方法:

PHP代码
  1. $rsi = new Utils_Caption();  
  2. $rsi->TFontSize=array(15,17);  
  3. $rsi->Width=50;  
  4. $rsi->Height=25;  
  5. $code = $rsi->RandRSI();  
  6. session_start();  
  7. $_SESSION["CHECKCODE"] = $code;  
  8. $rsi->Draw();  

 

原文链接:http://www.cnblogs.com/chengmo/archive/2010/12/06/1898062.html

点击下载此文件点击下载此文件



上一篇: php获取中文首字母
下一篇: 突然发现IIS日志和本地时间不符
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2358
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑