PHP实现的封装验证码类详解


Posted in PHP onJune 18, 2013

用PHP写一个验证码类,并进行封装。
类名: validationcode.class.php
代码如下:

<?php
 class ValidationCode {
  private $width;
  private $height;
  private $codeNum;
  private $image;   //图像资源
  private $disturbColorNum;
  private $checkCode;
  function __construct($width=80, $height=20, $codeNum=4){
   $this->width=$width;
   $this->height=$height;
   $this->codeNum=$codeNum;
   $this->checkCode=$this->createCheckCode();
   $number=floor($width*$height/15);   if($number > 240-$codeNum){
    $this->disturbColorNum= 240-$codeNum;
   }else{
    $this->disturbColorNum=$number;
   }
  }
  //通过访问该方法向浏览器中输出图像
  function showImage($fontFace=""){
   //第一步:创建图像背景
   $this->createImage();
   //第二步:设置干扰元素
   $this->setDisturbColor();
   //第三步:向图像中随机画出文本
   $this->outputText($fontFace);
   //第四步:输出图像
   $this->outputImage();
  }
  //通过调用该方法获取随机创建的验证码字符串
  function getCheckCode(){
   return $this->checkCode;
  }
  private function createImage(){
   //创建图像资源
   $this->image=imagecreatetruecolor($this->width, $this->height);
   //随机背景色
   $backColor=imagecolorallocate($this->image, rand(225, 255), rand(225,255), rand(225, 255));
   //为背景添充颜色
   imagefill($this->image, 0, 0, $backColor);
   //设置边框颜色
   $border=imagecolorallocate($this->image, 0, 0, 0);
   //画出矩形边框
   imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
  }
  private function  setDisturbColor(){
   for($i=0; $i<$this->disturbColorNum; $i++){
    $color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color);
   }
   for($i=0; $i<10; $i++){
    $color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));
    imagearc($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20, 200), 55, 44, $color);
   }
  }
  private function createCheckCode(){
//这里主要产生随机码,从2开始是为了区分1和l
   $code="23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
   $string='';
   for($i=0; $i < $this->codeNum; $i++){
    $char=$code{rand(0, strlen($code)-1)};
    $string.=$char;
   }
   return $string;
  }
  private function outputText($fontFace=""){
   for($i=0; $i<$this->codeNum; $i++){
    $fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128));
    if($fontFace==""){
     $fontsize=rand(3, 5);
     $x=floor($this->width/$this->codeNum)*$i+3;
     $y=rand(0, $this->height-15);
     imagechar($this->image,$fontsize, $x, $y, $this->checkCode{$i},$fontcolor);
    }else{
     $fontsize=rand(12, 16);
     $x=floor(($this->width-8)/$this->codeNum)*$i+8;
     $y=rand($fontSize+5, $this->height);
     imagettftext($this->image,$fontsize,rand(-30, 30),$x,$y ,$fontcolor, $fontFace, $this->checkCode{$i});
    }
   }
  }
  private function outputImage() {
   if(imagetypes() & IMG_GIF){
    header("Content-Type:image/gif");
    imagepng($this->image);
   }else if(imagetypes() & IMG_JPG){
    header("Content-Type:image/jpeg");
    imagepng($this->image);
   }else if(imagetypes() & IMG_PNG){
    header("Content-Type:image/png");
    imagepng($this->image);
   }else if(imagetypes() & IMG_WBMP){
    header("Content-Type:image/vnd.wap.wbmp");
    imagepng($this->image);
   }else{
    die("PHP不支持图像创建");
   }
  }
  function __destruct(){
   imagedestroy($this->image);
  }
 }

使用如下:
测试,调用验证码类
code.php
<?php
session_start();
include "validationcode.class.php";
$code=new ValidationCode(80, 20, 4);
$code->showImage();   //输出到页面中供 注册或登录使用
$_SESSION["code"]=$code->getCheckCode();  //将验证码保存到服务器中

PHP 相关文章推荐
php判断字符以及字符串的包含方法属性
Aug 30 PHP
PHP echo,print,printf,sprintf函数之间的区别与用法详解
Nov 27 PHP
php curl post 时出现的问题解决
Jan 30 PHP
php以post形式发送xml的方法
Nov 04 PHP
php中call_user_func函数使用注意事项
Nov 21 PHP
php绘图之加载外部图片的方法
Jan 24 PHP
php文件操作之小型留言本实例
Jun 20 PHP
thinkphp3.x中变量的获取和过滤方法详解
May 20 PHP
php版微信公众平台接口参数调试实现判断用户行为的方法
Sep 23 PHP
Symfony2创建基于域名的路由相关示例
Nov 14 PHP
PHP使用数组实现矩阵数学运算的方法示例
May 29 PHP
windows 2008r2+php5.6.28环境搭建详细过程
Jun 18 PHP
php empty()与isset()区别的详细介绍
Jun 17 #PHP
php include和require的区别深入解析
Jun 17 #PHP
浅析php header 跳转
Jun 17 #PHP
解析php中heredoc的使用方法
Jun 17 #PHP
深入PHP5中的魔术方法详解
Jun 17 #PHP
php.ini 配置文件的深入解析
Jun 17 #PHP
解析posix与perl标准的正则表达式区别
Jun 17 #PHP
You might like
第二章 PHP入门基础之php代码写法
2011/12/30 PHP
PHP的SQL注入过程分析
2012/01/06 PHP
php使用curl访问https示例分享
2014/01/17 PHP
常用PHP框架功能对照表
2014/10/23 PHP
ThinkPHP公共配置文件与各自项目中配置文件组合的方法
2014/11/24 PHP
搭建Vim为自定义的PHP开发工具的一些技巧
2015/12/11 PHP
TNC vs RR BO3 第一场 2.14
2021/03/10 DOTA
表单提交验证类
2006/07/14 Javascript
jquery 插件开发方法小结
2009/10/23 Javascript
js实现一个省市区三级联动选择框代码分享
2013/03/06 Javascript
Javascript 浮点运算精度问题分析与解决
2014/03/26 Javascript
JS判断移动端访问设备并加载对应CSS样式
2014/06/13 Javascript
BOOTSTRAP时间控件显示在模态框下面的bug修复
2015/02/05 Javascript
js+css实现select的美化效果
2016/03/24 Javascript
BootStrap便签页的简单应用
2017/01/06 Javascript
详解JavaScript中return的用法
2017/05/08 Javascript
使用Nuxt.js改造已有项目的方法
2018/08/07 Javascript
解决layui调用自定义方法提示未定义的问题
2019/09/14 Javascript
Javascript前端下载后台传来的文件流代码实例
2020/08/18 Javascript
[55:54]FNATIC vs EG 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
用Python编写简单的微博爬虫
2016/03/04 Python
Python模块文件结构代码详解
2018/02/03 Python
pytorch + visdom CNN处理自建图片数据集的方法
2018/06/04 Python
Python实现定时执行任务的三种方式简单示例
2019/03/30 Python
django之使用celery-把耗时程序放到celery里面执行的方法
2019/07/12 Python
Python 元组操作总结
2019/09/18 Python
Docker部署Python爬虫项目的方法步骤
2020/01/19 Python
python wav模块获取采样率 采样点声道量化位数(实例代码)
2020/01/22 Python
Python爬虫爬取ts碎片视频+验证码登录功能
2021/02/22 Python
工业自动化专业毕业生推荐信
2013/11/18 职场文书
博士学位自我鉴定范文
2013/12/26 职场文书
三年级评语大全
2014/04/23 职场文书
教师竞聘上岗演讲稿
2014/09/03 职场文书
2014年人民调解工作总结
2014/12/08 职场文书
医护人员继续教育学习心得体会
2016/01/19 职场文书
2016年教师学习教师法心得体会
2016/01/20 职场文书