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实现选择排序的解决方法
May 04 PHP
php中$美元符号与Zen Coding冲突问题解决方法分享
May 28 PHP
PHP 实现代码复用的一个方法 traits新特性
Feb 22 PHP
PHP时间和日期函数详解
May 08 PHP
PHP的文件操作与算法实现的面试题示例
Aug 10 PHP
PHP重定向与伪静态区别
Feb 19 PHP
yii框架无限极分类的实现方法
Apr 08 PHP
PHP基于递归实现的约瑟夫环算法示例
Aug 27 PHP
PHP实现从上往下打印二叉树的方法
Jan 18 PHP
PHP7引入的&quot;??&quot;和&quot;?:&quot;的区别讲解
Apr 08 PHP
在laravel中实现事务回滚的方法
Oct 10 PHP
php开发最强大的IDE编辑的phpstorm 2020.2配置Xdebug调试的详细教程
Aug 17 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
SONY ICF-F10中波修复记
2021/03/02 无线电
php 带逗号千位符数字的处理方法
2012/01/10 PHP
PHP实现的博客欢迎提示功能(很特别哦)
2014/06/05 PHP
PHP 常用的header头部定义汇总
2015/06/19 PHP
php计算年龄精准到年月日
2015/11/17 PHP
PHP函数rtrim()使用中的怪异现象分析
2017/02/24 PHP
JQuery文本改变触发事件如聚焦事件、失焦事件
2014/01/15 Javascript
JavaScript中setTimeout和setInterval函数的传参及调用
2016/03/11 Javascript
JS实现图片点击后出现模态框效果
2017/05/03 Javascript
JS实现自动轮播图效果(自适应屏幕宽度+手机触屏滑动)
2017/06/19 Javascript
JavaScript 值类型和引用类型的初次研究(推荐)
2017/07/19 Javascript
JS实现的简单四则运算计算器功能示例
2017/09/27 Javascript
简述Angular 5 快速入门
2017/11/04 Javascript
vue绑定事件后获取绑定事件中的this方法
2018/09/15 Javascript
vue实现一个炫酷的日历组件
2018/10/08 Javascript
vue中使用cookies和crypto-js实现记住密码和加密的方法
2018/10/18 Javascript
angular中两种表单的区别(响应式和模板驱动表单)
2018/12/06 Javascript
如何解决.vue文件url引用文件的问题
2019/01/18 Javascript
微信小程序学习总结(一)项目创建与目录结构分析
2020/06/04 Javascript
以Python的Pyspider为例剖析搜索引擎的网络爬虫实现方法
2015/03/30 Python
Python常用的文件及文件路径、目录操作方法汇总介绍
2015/05/21 Python
Selenium元素的常用操作方法分析
2018/08/10 Python
在PyCharm下打包*.py程序成.exe的方法
2018/11/29 Python
用python3读取python2的pickle数据方式
2019/12/25 Python
python中可以声明变量类型吗
2020/06/18 Python
Python unittest装饰器实现原理及代码
2020/09/08 Python
社团活动策划书范文
2014/01/09 职场文书
高中教师先进事迹材料
2014/08/22 职场文书
离婚协议书应该怎么写
2014/10/12 职场文书
超市采购员岗位职责
2015/04/07 职场文书
退休欢送会主持词
2015/07/01 职场文书
家访教师心得体会
2016/01/23 职场文书
小学2016年“我们的节日·重阳节”活动总结
2016/04/01 职场文书
确保减税降费落地生根,用实实在在措施
2019/07/19 职场文书
Go语言操作数据库及其常规操作的示例代码
2021/04/21 Golang
基于Golang 高并发问题的解决方案
2021/05/08 Golang