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下将XML转换为数组
Jan 01 PHP
php下获取客户端ip地址的函数
Mar 15 PHP
PHP操作数组的一些函数整理介绍
Jul 17 PHP
php动态生成函数示例
Mar 21 PHP
Codeigniter中禁止A Database Error Occurred错误提示的方法
Jun 12 PHP
ThinkPHP单字母函数(快捷方法)使用总结
Jul 23 PHP
PHP易混淆函数的区别及用法汇总
Nov 22 PHP
php截取指定2个字符之间字符串的方法
Apr 15 PHP
php+jQuery+Ajax实现点赞效果的方法(附源码下载)
Jul 21 PHP
利用PHP获取汉字首字母并且分组排序详解
Oct 22 PHP
PHP读取并输出XML文件数据的简单实现方法
Dec 22 PHP
PHP实现搜索时记住状态的方法示例
May 11 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中的静态变量的基本用法
2014/03/20 PHP
js监听表单value的修改同步问题,跨浏览器支持
2009/12/31 Javascript
file模式访问网页时iframe高度自适应解决方案
2013/01/16 Javascript
使用mouse事件实现简单的鼠标经过特效
2015/01/30 Javascript
javascript禁止超链接跳转的方法
2016/02/02 Javascript
AngularJS全局scope与Isolate scope通信用法示例
2016/11/22 Javascript
javascript中的深复制详解及实例分析
2016/12/29 Javascript
Angular下H5上传图片的方法(可多张上传)
2017/01/09 Javascript
nodejs学习笔记之路由
2017/03/27 NodeJs
jQuery实现radio第一次点击选中第二次点击取消功能
2017/05/15 jQuery
JS时间控制实现动态效果的实例讲解
2017/07/31 Javascript
vue+socket.io+express+mongodb 实现简易多房间在线群聊示例
2017/10/21 Javascript
解决vue2.0动态绑定图片src属性值初始化时报错的问题
2018/03/14 Javascript
npm 更改默认全局路径以及国内镜像的方法
2018/05/16 Javascript
node.js实现为PDF添加水印的示例代码
2018/12/05 Javascript
详解vue-cli3 中跨域解决方案
2019/04/10 Javascript
Vue使用watch监听一个对象中的属性的实现方法
2019/05/10 Javascript
js实现随机点名
2021/01/19 Javascript
python中mechanize库的简单使用示例
2014/01/10 Python
通过C++学习Python
2015/01/20 Python
python创建临时文件夹的方法
2015/07/06 Python
Python基于matplotlib绘制栈式直方图的方法示例
2017/08/09 Python
python实现八大排序算法(1)
2017/09/14 Python
python将txt文件读入为np.array的方法
2018/10/30 Python
使用python读取.text文件特定行的数据方法
2019/01/28 Python
基于Python实现大文件分割和命名脚本过程解析
2019/09/29 Python
Python任务自动化工具tox使用教程
2020/03/17 Python
Python中使用socks5设置全局代理的方法示例
2020/04/15 Python
CSS3实现多重边框的方法总结
2016/05/31 HTML / CSS
SCDKey德国:全球领先的数字游戏市场
2019/04/09 全球购物
递归实现回文判断(如:abcdedbca就是回文,判断一个面试者对递归理解的简单程序)
2013/04/28 面试题
网上常见的一份Linux面试题(多项选择部分)
2014/09/09 面试题
十佳大学生事迹材料
2014/01/29 职场文书
物流专业自荐信
2014/05/23 职场文书
孔庙导游词
2015/02/04 职场文书
mybatis中注解与xml配置的对应关系和对比分析
2021/08/04 Java/Android