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语法速查表
Dec 06 PHP
php url地址栏传中文乱码解决方法集合
Jun 25 PHP
PHP原生模板引擎 最简单的模板引擎
Apr 25 PHP
解析mysql left( right ) join使用on与where筛选的差异
Jun 18 PHP
PHP中使用CURL模拟登录并获取数据实例
Jul 01 PHP
基于PHP实现通过照片获取ip地址
Apr 26 PHP
PHP将页面中点击数量高的链接进行高亮显示的方法
May 30 PHP
PHP实现递归目录的5种方法
Oct 27 PHP
php中static 静态变量和普通变量的区别
Dec 01 PHP
PHP调试及性能分析工具Xdebug详解
Feb 09 PHP
Laravel 中创建 Zip 压缩文件并提供下载的实现方法
Apr 02 PHP
PhpStorm的使用教程(本地运行PHP+远程开发+快捷键)
Mar 26 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的分页功能
2007/03/21 PHP
jQuery EasyUI API 中文文档 - DateBox日期框
2011/10/15 PHP
Thinkphp5.0自动生成模块及目录的方法详解
2017/04/17 PHP
Laravel框架自定义分页样式操作示例
2020/01/26 PHP
JS 拼图游戏 面向对象,注释完整。
2009/06/18 Javascript
复制js对象方法(详解)
2013/07/08 Javascript
jquery提取元素里的纯文本不包含span等里的内容
2013/09/30 Javascript
js定时器怎么写?就是在特定时间执行某段程序
2013/10/11 Javascript
js自动生成的元素与页面原有元素发生堆叠的解决方法
2013/10/24 Javascript
angularJS 入门基础
2015/02/09 Javascript
JavaScript中使用Callback控制流程介绍
2015/03/16 Javascript
深入理解vue中的$set
2017/06/01 Javascript
jquery DataTable实现前后台动态分页
2017/06/17 jQuery
vue页面跳转后返回原页面初始位置方法
2018/02/11 Javascript
微信小程序上传图片功能(附后端代码)
2020/06/19 Javascript
Express结合Webpack的全栈自动刷新
2019/05/23 Javascript
解决vue打包后vendor.js文件过大问题
2019/07/03 Javascript
浅谈bootstrap layer.open中end的使用方法
2019/09/12 Javascript
layui 上传图片 返回图片地址的方法
2019/09/26 Javascript
react用Redux中央仓库实现一个todolist
2019/09/29 Javascript
python使用Queue在多个子进程间交换数据的方法
2015/04/18 Python
Python字符串内置函数功能与用法总结
2019/04/16 Python
如何用Python破解wifi密码过程详解
2019/07/12 Python
关于numpy.where()函数 返回值的解释
2019/12/06 Python
Python之字符串的遍历的4种方式
2020/12/08 Python
使用CSS3设计地图上的雷达定位提示效果
2016/04/05 HTML / CSS
美国百货齐全的精品网站,提供美式风格的产品:Overstock.com
2016/07/22 全球购物
Senreve官网:美国旧金山的奢侈手袋品牌
2019/03/21 全球购物
沙特阿拉伯家用电器和电子产品购物网站:Sheta and Saif
2020/04/03 全球购物
土木工程师岗位职责
2013/11/24 职场文书
经理助理岗位职责
2014/03/05 职场文书
小学课外阅读总结
2014/07/09 职场文书
煤矿安全保证书
2015/02/27 职场文书
小学入学感言
2015/08/01 职场文书
Oracle 临时表空间SQL语句的实现
2021/09/25 Oracle
python文件与路径操作神器 pathlib
2022/04/01 Python