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 相关文章推荐
新浪新闻小偷
Oct 09 PHP
PHP 存储文本换行实现方法
Jan 05 PHP
分享常见的几种页面静态化的方法
Jan 08 PHP
php猴子选大王问题解决方法
May 12 PHP
深入理解PHP变量的值类型和引用类型
Oct 21 PHP
详解PHP对数组的定义以及数组的创建方法
Nov 27 PHP
php无法连接mysql数据库的正确解决方法
Jul 01 PHP
php版微信公众平台开发之验证步骤实例详解
Sep 23 PHP
php生成毫秒时间戳的实例讲解
Sep 22 PHP
用Laravel Sms实现laravel短信验证码的发送的实现
Nov 29 PHP
PHP连接MySQL数据库的三种方式实例分析【mysql、mysqli、pdo】
Nov 04 PHP
PHP xpath提取网页数据内容代码解析
Jul 16 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
第二节 对象模型 [2]
2006/10/09 PHP
利用PHP实现图片等比例放大和缩小的方法详解
2013/06/06 PHP
各种快递查询--Api接口
2016/04/26 PHP
php实现多维数组排序的方法示例
2017/03/23 PHP
php实现用户注册密码的crypt加密
2017/06/08 PHP
in.js 一个轻量级的JavaScript颗粒化模块加载和依赖关系管理解决方案
2011/07/26 Javascript
最好用的省市二级联动 原生js实现你值得拥有
2013/09/22 Javascript
Jquery实现控件的隐藏和显示实例
2014/02/08 Javascript
编程语言JavaScript简介
2014/10/16 Javascript
JavaScript strike方法入门实例(给字符串加上删除线)
2014/10/17 Javascript
JavaScript Window浏览器对象模型方法与属性汇总
2015/04/20 Javascript
jquery实现简单的瀑布流布局
2016/12/11 Javascript
求js数组的最大值和最小值的四种方法
2017/03/03 Javascript
JS实现简单拖拽效果
2017/06/21 Javascript
基于vue 实现token验证的实例代码
2017/12/14 Javascript
vue  自定义组件实现通讯录功能
2018/09/30 Javascript
Angular6使用forRoot() 注册单一实例服务问题
2019/08/27 Javascript
easyUI 实现的后台分页与前台显示功能示例
2020/06/01 Javascript
vue实现图书管理系统
2020/12/29 Vue.js
python之模拟鼠标键盘动作具体实现
2013/12/30 Python
python版本五子棋的实现代码
2018/12/11 Python
详解python数据结构和算法
2019/04/18 Python
Python Web框架之Django框架cookie和session用法分析
2019/08/16 Python
Python hashlib加密模块常用方法解析
2019/12/18 Python
python str字符串转uuid实例
2020/03/03 Python
python的help函数如何使用
2020/06/11 Python
为2021年的第一场雪锦上添花:用matplotlib绘制雪花和雪景
2021/01/05 Python
小天鹅官方商城:LittleSwan
2017/06/16 全球购物
Solid & Striped官网:美国泳装品牌
2019/06/19 全球购物
美国手机支架公司:PopSockets
2019/11/27 全球购物
音乐系毕业生自荐信
2013/10/27 职场文书
软件项目开发计划书
2014/05/01 职场文书
2015年小学图书室工作总结
2015/05/18 职场文书
永远是春天观后感
2015/06/12 职场文书
健身房被搭讪?用python写了个小米计时器助人为乐
2021/06/08 Python
SpringBoot集成Redis的思路详解
2021/10/16 Redis