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 相关文章推荐
BBS(php &amp; mysql)完整版(二)
Oct 09 PHP
php&amp;java(二)
Oct 09 PHP
理解PHP5中static和const关键字的区别
Mar 19 PHP
php执行sql语句的写法
Mar 10 PHP
php数据结构 算法(PHP描述) 简单选择排序 simple selection sort
Aug 09 PHP
php实现简单洗牌算法
Jun 18 PHP
PHP清除数组中所有字符串两端空格的方法
Oct 20 PHP
Win7下手动安装apache2.2、php5.4笔记
Apr 03 PHP
抛弃 PHP 代价太高
Apr 26 PHP
php错误日志简单配置方法
Jul 11 PHP
PHP流Streams、包装器wrapper概念与用法实例详解
Nov 17 PHP
php微信开发之音乐回复功能
Jun 14 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
PHP4实际应用经验篇(1)
2006/10/09 PHP
浅析PHP页面局部刷新功能的实现小结
2013/06/21 PHP
[原创]PHP简单开启curl的方法(测试可行)
2016/01/11 PHP
JS保留两位小数,多位小数的示例代码
2014/01/07 Javascript
jQuery中;function($,undefined) 前面的分号的用处
2014/12/17 Javascript
jquery增加和删除元素的方法
2015/01/14 Javascript
DOM基础教程之使用DOM
2015/01/19 Javascript
jQuery插件HighCharts实现2D柱状图、折线图的组合多轴图效果示例【附demo源码下载】
2017/03/09 Javascript
React-Native之定时器Timer的实现代码
2017/10/04 Javascript
JavaScript实现二叉树定义、遍历及查找的方法详解
2017/12/20 Javascript
使用npm安装最新版本nodejs
2018/01/18 NodeJs
最适应的vue.js的form提交涉及多种插件【推荐】
2018/08/27 Javascript
JS实现处理时间,年月日,星期的公共方法示例
2019/05/31 Javascript
Layer组件多个iframe弹出层打开与关闭及参数传递的方法
2019/09/25 Javascript
JS call()及apply()方法使用实例汇总
2020/07/11 Javascript
Vue 实现创建全局组件,并且使用Vue.use() 载入方式
2020/08/11 Javascript
[02:39]DOTA2英雄基础教程 极限穿梭编织者
2013/12/05 DOTA
[41:08]TNC vs VG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python中使用enumerate函数遍历元素实例
2014/06/16 Python
Python实现去除代码前行号的方法
2015/03/10 Python
python在指定目录下查找gif文件的方法
2015/05/04 Python
Python中模块string.py详解
2017/03/12 Python
对Python+opencv将图片生成视频的实例详解
2019/01/08 Python
python使用PIL实现多张图片垂直合并
2019/01/15 Python
Python设计模式之模板方法模式实例详解
2019/01/17 Python
解决pycharm不能自动补全第三方库的函数和属性问题
2020/03/12 Python
详解CSS3 弹性布局快速入门
2019/06/06 HTML / CSS
瑞典在互联网上最大的宠物商店:Animail
2020/10/31 全球购物
PHP数据运算类型都有哪些
2013/11/05 面试题
公务员转正考察材料
2014/02/07 职场文书
《锄禾》教学反思
2014/04/08 职场文书
学历公证书范本
2014/04/09 职场文书
小学生寒假家长评语
2014/04/16 职场文书
党员四风自我剖析材料思想汇报
2014/09/13 职场文书
房屋出售授权委托书
2014/10/12 职场文书
2016国庆节67周年寄语
2015/12/07 职场文书