php 生成验证码(带旋转和干扰线条)

带旋转和干扰线条的验证码生成函数,代码如下:

  1. <?php  
  2.     // 把字符串做出验证码图片  
  3.     // string $text,要处理的字符串  
  4.     // int $linect,需要混淆的线条个数  
  5.     // int $fontsize,字体大小,1到5  
  6.     // bool $rotate,是否旋转  
  7.     // array $textcol,字体颜色RGB值,array(R,G,B)  
  8.     // array $bgcol,背景色RGB值,array(R,G,B)  
  9.     function verifyCode($text$linect=6, $fontsize=5, $rotate=array(-44,44), $textcol = array(255,0,0), $bgcol=array(255,255,255)){  
  10.         $len = strlen($text);  
  11.         $tximgw = imagefontwidth($fontsize);  
  12.         $tximgh = imagefontheight($fontsize);  
  13.   
  14.         $imgof = $tximgw/2;  
  15.         $imgw = $len*($tximgw+$imgof);  
  16.         $imgh = $tximgh;  
  17.           
  18.         $img = imagecreate($imgw$imgh);  
  19.   
  20.         imagefill($img, 0,0, imagecolorallocate($img$bgcol[0], $bgcol[1], $bgcol[2]));  
  21.           
  22.         // 输出字符  
  23.         for($i=0; $i<$len; ++$i){  
  24.             $char = $text[$i];  
  25.             $tximg = imagecreate($tximgw$tximgh);  
  26.             $txbgcol = imagecolorallocate($tximg$bgcol[0], $bgcol[1], $bgcol[2]);  
  27.             $txcol = imagecolorallocate($tximg$textcol[0], $textcol[1], $textcol[2]);  
  28.             imagestring($tximg$fontsize, 0, 0, $char$txcol);  
  29.             $ag = $rotate?rand($rotate[0],$rotate[1]):0;  
  30.               
  31.             // 旋转字符随机角度  
  32.             $oimg = imagerotate($tximg$ag$txbgcol);  
  33.             imagecopy($img$oimg$i*($tximgw+$imgof), 0 , 0 , 0 , imagesx($oimg) , imagesy($oimg));  
  34.               
  35.             // 释放临时产生的图片资源  
  36.             imagedestroy($tximg);  
  37.             imagedestroy($oimg);  
  38.         }  
  39.           
  40.         // 产生混淆的线条  
  41.         for($i=0; $i<$linect; ++$i){  
  42.             $x = rand(0, $imgw);  
  43.             $y = rand(0, $imgh);  
  44.             $x1 = rand(0, $imgw);  
  45.             $y1 = rand(0, $imgh);  
  46.             imageline ($img$x$y$x1$y1, imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)));  
  47.         }  
  48.         return $img;  
  49.     }  
  50.     // 测试  
  51.     $text = 'HelloWorld';  
  52.     // 产生一个有5个混淆线条,字体为5,旋转角度在[-30,30]间的验证码  
  53.     $img = verifyCode($text, 5, 5, array(-30, 30));  
  54.     // 设置输出类型为png图片  
  55.     header("Content-type: image/png");  
  56.     // 输出  
  57.     imagepng($img);  
  58.     // 释放资源  
  59.     imagedestroy($img);  
  60. ?>  


效果如图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值