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垃圾回收机制简单说明
Jul 22 PHP
php入门学习知识点八 PHP中for循环基本应用之九九乘法口绝表
Jul 14 PHP
php中使用redis队列操作实例代码
Feb 07 PHP
基于curl数据采集之单页面采集函数get_html的使用
Apr 28 PHP
php获取汉字首字母的函数
Nov 07 PHP
php5.2 Json不能正确处理中文、GB编码的解决方法
Mar 28 PHP
自己写的php中文截取函数mb_strlen和mb_substr
Feb 09 PHP
php接口数据加密、解密、验证签名
Mar 12 PHP
php邮件发送的两种方式
Apr 28 PHP
WordPress中自定义后台管理界面配色方案的小技巧
Dec 29 PHP
php制作圆形用户头像的实例_自定义封装类源代码
Sep 18 PHP
PHP超全局变量实现原理及代码解析
Sep 01 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
德生1994机评
2021/03/02 无线电
php 引用(&amp;)详解
2009/11/20 PHP
PHP中判断变量为空的几种方法分享
2013/08/26 PHP
PHP中time(),date(),mktime()区别介绍
2013/09/28 PHP
php对数组内元素进行随机调换的方法
2015/05/12 PHP
PHP房贷计算器实例代码,等额本息,等额本金
2017/04/01 PHP
PHP laravel中的多对多关系实例详解
2017/06/07 PHP
showModalDialog 和 showModelessDialog
2007/01/22 Javascript
js继承 Base类的源码解析
2008/12/30 Javascript
JQuery实现超链接鼠标提示效果的方法
2015/06/10 Javascript
jQuery实现的个性化返回底部与返回顶部特效代码
2015/10/30 Javascript
浅谈jquery设置和获得checkbox选中的问题
2016/08/19 Javascript
jQuery常见面试题之DOM操作详析
2017/07/05 jQuery
BootStrap实现文件上传并带有进度条效果
2017/09/11 Javascript
JavaScript实现左侧菜单效果
2017/12/14 Javascript
详解vue指令与$nextTick 操作DOM的不同之处
2018/08/02 Javascript
在Vue 中使用Typescript的示例代码
2018/09/10 Javascript
微信小程序下拉刷新PullDownRefresh的使用方法
2018/11/29 Javascript
Vue起步(无cli)的啊教程详解
2019/04/11 Javascript
vue-preview动态获取图片宽高并增加旋转功能的实现
2020/07/29 Javascript
python的绘图工具matplotlib使用实例
2014/07/03 Python
Python中使用不同编码读写txt文件详解
2015/05/28 Python
python实现树形打印目录结构
2018/03/29 Python
opencv python 基于KNN的手写体识别的实例
2018/08/03 Python
flask 实现上传图片并缩放作为头像的例子
2020/01/09 Python
Python3读写Excel文件(使用xlrd,xlsxwriter,openpyxl3种方式读写实例与优劣)
2020/02/13 Python
关于torch.optim的灵活使用详解(包括重写SGD,加上L1正则)
2020/02/20 Python
python绘图pyecharts+pandas的使用详解
2020/12/13 Python
想学画画?python满足你!
2020/12/24 Python
python中time包实例详解
2021/02/02 Python
Canvas实现放大镜效果完整案例分析(附代码)
2020/11/26 HTML / CSS
热能动力工程毕业生自荐信
2013/11/07 职场文书
北京颐和园导游词
2015/01/30 职场文书
开天辟地观后感
2015/06/09 职场文书
安全教育观后感
2015/06/17 职场文书
Kubernetes控制节点的部署
2022/04/01 Servers