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简单系统数据添加以及数据删除模块源文件下载
Jun 07 PHP
php数据结构与算法(PHP描述) 查找与二分法查找
Jun 21 PHP
php创建基本身份认证站点的方法详解
Jun 08 PHP
使用PHP遍历文件目录与清除目录中文件的实现详解
Jun 24 PHP
php定界符
Jun 19 PHP
ThinkPHP查询语句与关联查询用法实例
Nov 01 PHP
php上传文件并存储到mysql数据库的方法
Mar 16 PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
Apr 21 PHP
PHP6新特性分析
Mar 03 PHP
PHP中Cookie的使用详解(简单易懂)
Apr 28 PHP
PHP赋值的内部是如何跑的详解
Jan 13 PHP
PHP pthreads v3下同步处理synchronized用法示例
Feb 21 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调用MySQL的存储过程的实现代码
2008/08/12 PHP
PHP explode()函数用法、切分字符串
2012/10/03 PHP
基于PHP输出缓存(output_buffering)的深入理解
2013/06/13 PHP
分享PHP计算两个日期相差天数的代码
2015/12/23 PHP
PHP程序中的文件锁、互斥锁、读写锁使用技巧解析
2016/03/21 PHP
yii2分页之实现跳转到具体某页的实例代码
2016/06/02 PHP
Javascript 复制数组实现代码
2009/11/26 Javascript
深入理解Javascript闭包 新手版
2010/12/28 Javascript
有关于JS辅助函数inherit()的问题
2013/04/07 Javascript
JQueryEasyUI datagrid框架的基本使用
2013/04/08 Javascript
jquery prop的使用介绍及与attr的区别
2013/12/19 Javascript
js的flv视频播放器插件使用方法
2015/06/23 Javascript
jquery.mousewheel实现整屏翻屏效果
2015/08/30 Javascript
js实现倒计时效果(小于10补零)
2017/03/08 Javascript
Require.JS中的几种define定义方式示例
2017/06/01 Javascript
AngularJS基于provider实现全局变量的读取和赋值方法
2017/06/28 Javascript
JavaScript创建对象的常用方式总结
2018/08/10 Javascript
Canvas实现微信红包照片效果
2018/08/21 Javascript
JS实现的tab切换并显示相应内容模块功能示例
2019/08/03 Javascript
关于Js中new操作符的作用详解
2021/02/21 Javascript
[58:57]2018DOTA2亚洲邀请赛3月29日小组赛B组 Effect VS VGJ.T
2018/03/30 DOTA
Python算法应用实战之队列详解
2017/02/04 Python
对Python中type打开文件的方式介绍
2018/04/28 Python
Django项目主urls导入应用中views的红线问题解决
2019/08/10 Python
keras训练浅层卷积网络并保存和加载模型实例
2020/07/02 Python
matplotlib 多个图像共用一个colorbar的实现示例
2020/09/10 Python
HTML5 Plus 实现手机APP拍照或相册选择图片上传功能
2016/07/13 HTML / CSS
标签和贴纸印刷:Lightning Labels
2018/03/22 全球购物
领导接待方案
2014/03/13 职场文书
高三毕业寄语
2014/04/10 职场文书
会计专业求职信
2014/08/10 职场文书
2014七年级班主任工作总结
2014/12/05 职场文书
预备党员自我评价范文
2015/03/04 职场文书
2015年大学社团工作总结
2015/04/09 职场文书
2015年新教师个人工作总结
2015/10/14 职场文书
交互式可视化js库gojs使用介绍及技巧
2022/02/18 Javascript