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 相关文章推荐
解决GD中文乱码问题
Feb 14 PHP
从MySQL数据库表中取出随机数据的代码
Sep 05 PHP
PHP 常用函数库和一些实用小技巧
Jan 01 PHP
Php中用PDO查询Mysql来避免SQL注入风险的方法
Apr 25 PHP
PHP中使用gettext解决国际化问题的例子(i18n)
Jun 13 PHP
Yii实现MySQL多数据库和读写分离实例分析
Dec 03 PHP
PHP中的命名空间相关概念浅析
Jan 22 PHP
教大家制作简单的php日历
Nov 17 PHP
PHP的Laravel框架中使用AdminLTE模板来编写网站后台界面
Mar 21 PHP
laravel手动创建数组分页的实现代码
Jun 07 PHP
php中目录操作opendir()、readdir()及scandir()用法示例
Jun 08 PHP
laravel框架实现为 Blade 模板引擎添加新文件扩展名操作示例
Jan 25 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图片上传类带图片显示
2006/11/25 PHP
关于js与php互相传值的介绍
2013/06/25 PHP
PHP实现对二维数组某个键排序的方法
2016/09/14 PHP
Yii框架中jquery表单验证插件用法示例
2016/10/18 PHP
javascript CSS画图之基础篇
2009/07/29 Javascript
jquery 清空file域示例(兼容个浏览器)
2013/10/11 Javascript
node.js正则表达式获取网页中所有链接的代码实例
2014/06/03 Javascript
javascript关于继承的用法汇总
2014/12/20 Javascript
jQuery层级选择器用法分析
2015/02/10 Javascript
jQuery事件委托之Safari
2016/07/05 Javascript
微信小程序 教程之WXML
2016/10/18 Javascript
详解使用vue-router进行页面切换时滚动条位置与滚动监听事件
2017/03/08 Javascript
Javascript三种字符串连接方式及性能比较
2019/05/28 Javascript
python实现在字符串中查找子字符串的方法
2015/07/11 Python
Python解析json文件相关知识学习
2016/03/01 Python
Django应用程序中如何发送电子邮件详解
2017/02/04 Python
利用pandas读取中文数据集的方法
2018/07/25 Python
Python 常用模块 re 使用方法详解
2019/06/06 Python
Python中print函数简单使用总结
2019/08/05 Python
Pytorch 实现计算分类器准确率(总分类及子分类)
2020/01/18 Python
python代码区分大小写吗
2020/06/17 Python
踩坑:pytorch中eval模式下结果远差于train模式介绍
2020/06/23 Python
python3中TQDM库安装及使用详解
2020/11/18 Python
HTML5+CSS3实现拖放(Drag and Drop)示例
2014/07/07 HTML / CSS
WoolOvers爱尔兰:羊绒、羊毛和棉针织品
2017/01/04 全球购物
美国顶级防滑鞋:Shoes For Crews
2017/03/27 全球购物
网络教育自我鉴定
2014/02/04 职场文书
《最可爱的人》教学反思
2014/02/14 职场文书
财政专业求职信范文
2014/02/19 职场文书
小学班主任评语大全
2014/04/23 职场文书
纪念九一八爱国演讲稿600字
2014/09/14 职场文书
单位委托书格式范本
2014/09/29 职场文书
大一新生军训新闻稿
2015/07/17 职场文书
nginx处理http请求实现过程解析
2021/03/31 Servers
nginx从安装到配置详细说明(安装,安全配置,防盗链,动静分离,配置 HTTPS,性能优化)
2022/02/12 Servers
Android Gradle 插件自定义Plugin实现注意事项
2022/06/16 Java/Android