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 相关文章推荐
WINDOWS 2000下使用ISAPI方式安装PHP
Sep 05 PHP
php文件打包 下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
Jun 13 PHP
codeigniter使用技巧批量插入数据实例方法分享
Dec 31 PHP
php sybase_fetch_array使用方法
Apr 15 PHP
又一个PHP实现的冒泡排序算法分享
Aug 21 PHP
smarty缓存用法分析
Dec 16 PHP
Symfony学习十分钟入门经典教程
Feb 03 PHP
利用PHP实现开心消消乐的算法示例
Oct 12 PHP
PHP 中魔术常量的实例详解
Oct 26 PHP
PHP基于回溯算法解决n皇后问题的方法示例
Nov 07 PHP
php判断电子邮件是否正确方法
Dec 04 PHP
PHP实现腾讯短网址生成api接口实例
Dec 08 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安全性漫谈
2012/06/28 PHP
php切割页面div内容的实现代码分享
2012/07/31 PHP
浅析ThinkPHP的模板输出功能
2014/07/01 PHP
一个不易被发现的PHP后门代码解析
2014/07/05 PHP
PHP实现对png图像进行缩放的方法(支持透明背景)
2015/07/15 PHP
PHP中cookie知识点学习
2018/05/06 PHP
PHP中number_format()函数的用法讲解
2019/04/08 PHP
JavaScript-世界上误解最深的语言分析
2007/08/12 Javascript
超实用的JavaScript表单代码段
2016/02/26 Javascript
使用Object.defineProperty实现简单的js双向绑定
2016/04/15 Javascript
最佳的JavaScript错误处理实践
2016/07/16 Javascript
Bootstrap Table使用心得总结
2016/11/29 Javascript
windows下vue-cli导入bootstrap样式
2017/04/25 Javascript
ReactNative之键盘Keyboard的弹出与消失示例
2017/07/11 Javascript
Vue.js 单页面多路由区域操作的实例详解
2017/07/17 Javascript
js封装成插件的步骤方法
2017/09/11 Javascript
微信小程序使用template标签实现五星评分功能
2018/11/03 Javascript
jQuery实现的上拉刷新功能组件示例
2020/05/01 jQuery
[03:02]安得倚天剑,跨海斩长鲸——中国军团出征DOTA2国际邀请赛
2018/08/14 DOTA
详解Python的单元测试
2015/04/28 Python
python妙用之编码的转换详解
2017/04/21 Python
python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法
2018/09/13 Python
python集合比较(交集,并集,差集)方法详解
2018/09/13 Python
10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径
2019/08/12 Python
Python自动化完成tb喵币任务的操作方法
2019/10/30 Python
pandas数据拼接的实现示例
2020/04/16 Python
全网最细 Python 格式化输出用法讲解(推荐)
2021/01/18 Python
英国著名的化妆品折扣网站:Allbeauty.com
2016/07/21 全球购物
美国领先的眼镜和太阳镜在线零售商:Glasses.com
2019/08/26 全球购物
Java里面如何创建一个内部类的实例
2015/01/19 面试题
分厂厂长岗位职责
2013/12/29 职场文书
学习心得体会
2014/01/01 职场文书
党的群众路线教育实践活动督导组工作情况汇报
2014/10/28 职场文书
2015年团支部工作总结
2015/04/03 职场文书
前端vue+express实现文件的上传下载示例
2022/02/18 Vue.js
CSS的calc函数用法小结
2022/06/25 HTML / CSS