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新手上路(七)
Oct 09 PHP
php cout&amp;lt;&amp;lt;的一点看法
Jan 24 PHP
php循环检测目录是否存在并创建(循环创建目录)
Jan 06 PHP
discuz免激活同步登入代码修改方法(discuz同步登录)
Dec 24 PHP
php递归遍历删除文件的方法
Apr 17 PHP
PHP内核探索:哈希表碰撞攻击原理
Jul 31 PHP
php如何实现只替换一次或N次
Oct 29 PHP
PHP5.5迭代生成器用法实例详解
Mar 16 PHP
Ubuntu server 11.04安装memcache及php使用memcache来存储session的方法
May 31 PHP
php自定义截取中文字符串-utf8版
Feb 27 PHP
YII框架关联查询操作示例
Apr 29 PHP
php中try catch捕获异常实例详解
Aug 06 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 session应用实例 登录验证
2009/03/16 PHP
php对大文件进行读取操作的实现代码
2013/01/23 PHP
php启用sphinx全文搜索的实现方法
2014/12/24 PHP
PHP中数据类型转换的三种方式
2015/04/02 PHP
PHP实现事件机制实例分析
2015/06/26 PHP
php实现表单多按钮提交action的处理方法
2015/10/24 PHP
Linux系统中设置多版本PHP共存配合Nginx服务器使用
2015/12/21 PHP
PHP使用递归算法无限遍历数组示例
2017/01/13 PHP
php解决安全问题的方法实例
2019/09/19 PHP
如何实现动态删除javascript函数
2007/05/27 Javascript
Prototype Hash对象 学习
2009/07/19 Javascript
小白谈谈对JS原型链的理解
2016/05/03 Javascript
Bootstrap编写导航栏和登陆框
2016/05/30 Javascript
原生js实现class的添加和删除简单代码
2016/07/12 Javascript
Javascript实现图片懒加载插件的方法
2016/10/20 Javascript
jquery实现简单的瀑布流布局
2016/12/11 Javascript
jquery Ajax 全局调用封装实例详解
2017/01/16 Javascript
Vue.js 插件开发详解
2017/03/29 Javascript
jQuery使用动画队列自定义动画操作示例
2018/06/16 jQuery
微信小程序自定义菜单切换栏tabbar组件代码实例
2019/12/30 Javascript
vue-路由精讲 二级路由和三级路由的作用
2020/08/06 Javascript
详解nginx配置vue h5 history去除#号
2020/11/09 Javascript
[03:10]超级美酒第四天 fy拉比克秀 大合集
2018/06/05 DOTA
python正则表达式抓取成语网站
2013/11/20 Python
Python 分析Nginx访问日志并保存到MySQL数据库实例
2014/03/13 Python
Python实现字符串的逆序 C++字符串逆序算法
2020/05/28 Python
Python之pymysql的使用小结
2019/07/01 Python
python中的列表与元组的使用
2019/08/08 Python
Anaconda之conda常用命令介绍(安装、更新、删除)
2019/10/06 Python
使用CSS3来代替JS实现交互
2017/08/10 HTML / CSS
波兰数码相机及配件网上商店: Cyfrowe.pl
2017/06/19 全球购物
贝尔帐篷精品店:Bell Tent Boutique
2019/06/12 全球购物
堂吉诃德读书笔记
2015/06/30 职场文书
股东出资协议书
2016/03/21 职场文书
不要在HTML中滥用div
2021/05/08 HTML / CSS
MySQL一劳永逸永久支持输入中文的方法实例
2022/08/05 MySQL