设为首页 收藏本站
查看: 1178|回复: 0

[经验分享] php写验证码类

[复制链接]

尚未签到

发表于 2015-8-23 15:04:40 | 显示全部楼层 |阅读模式
  前一段时间我写了个验证码函数, 今天做成了验证码类 有助于面向对象编程。
  img.php




1 <?php
2 /**
3 * QQ:279861795
4 * Author: gwyy
5 * Date: 2011-7-01
6 *通用验证码类
7 *版本:V0.1
8 */
9
10 class ValidateCode {
11      private  $charset="abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";   //随机因子  
12      private  $code;     //验证码文字
13      private  $codelen=4;    //验证码显示几个文字
14      private  $width=130;   //验证码宽度
15      private  $height=50;   //验证码高度
16      private  $img;       //验证码资源句柄
17      private  $font;     //指定的字体
18      private  $fontsize=20;  //指定的字体大小
19      private  $fontcolor;     //字体颜色  随机
20   
21      //构造类  编写字体
22      public  function __construct(){
23          $this->font=ROOT_PATH.'/font/elephant.ttf';
24      }
25      //创建4个随机码
26      private function createCode(){
27          $_leng=strlen($this->charset);
28          for($i=1;$i<=$this->codelen;$i++){
29              $this->code.=$this->charset[mt_rand(0,$_leng)];
30          }
31          return $this->code;
32      }
33     
34      //创建背景
35      private function createBg(){
36          //创建画布 给一个资源jubing
37          $this->img=imagecreatetruecolor($this->width,$this->height);
38          //背景颜色
39          $color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
40         //画出一个矩形
41         imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
42      }
43     
44      //创建字体
45      private  function createFont(){
46          $_x=($this->width / $this->codelen);   //字体长度
47          for ($i=0;$i<$this->codelen;$i++){
48              //文字颜色
49              $color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
50              //资源句柄 字体大小 倾斜度 字体长度  字体高度  字体颜色  字体  具体文本
51              imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$color,$this->font,$this->code[$i]);
52          }
53      }
54      //随机线条
55      private function createLine(){
56          //随机线条
57          for ($i=0;$i<6;$i++){
58              $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
59              imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
60          }
61          //随机雪花
62          for ($i=0;$i<45;$i++){
63              $color = imagecolorallocate($this->img,mt_rand(220,255),mt_rand(220,255),mt_rand(220,255));
64              imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
65          }
66      }
67      //输出背景
68      private  function outPut(){
69           //生成标头
70          header('ContentType:img/png');
71          //输出图片
72          imagepng($this->img);
73          //销毁结果集
74          imagedestroy($this->img);
75      }
76      //对外输出
77      public  function doimg(){
78          //加载背景
79          $this->createBg();
80          //加载文件
81          $this->createCode();
82          //加载线条
83          $this->createLine();
84          //加载字体
85          $this->createFont();
86          //加载背景
87          $this->outPut();
88  }
89
90 //获取验证码
91      public  function getCode(){
92          return strtolower($this->code);
93    }
94
95 }
96
97    ?>
  
  
  
  其他页面调用方法
  index.php
  
  <?php
  require 'img.php';
  $img=new ValidateCode();
  echo $img->doimg();

  ?>
  
  
                                                                                                                  过往云烟 2011/07/29

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-103058-1-1.html 上篇帖子: php同时使用session和cookie来保存用户登录信息 下篇帖子: nginx+php的配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表