PHP code 验证码生成类定义和简单使用示例


Posted in PHP onMay 27, 2020

本文实例讲述了PHP code 验证码生成类定义和简单使用。分享给大家供大家参考,具体如下:

code.php

<?php
namespace code;
/**
 * Class Code
 */
class Code
{
  protected $number;//验证码内字符个数
  protected $codeType;//验证码样式
  protected $width;//图像宽
  protected $height;//图像高
  protected $code;//验证码
  protected $image;//图像资源
 
  /**
   * Code constructor.
   * @param int $number
   * @param int $codeType
   * @param int $width
   * @param int $height
   */
  public function __construct($number=5, $codeType=2, $width=100, $height=40)
  {
    $this->number = $number;
    $this->codeType = $codeType;
    $this->width = $width;
    $this->height = $height;
    $this->code = $this->createCode();
  }
 
  /**
   * 销毁资源
   */
  public function __destruct()
  {
    imagedestroy($this->image);
  }
 
  /**
   * 外部调用code时触发
   * @param $name
   * @return bool
   */
  public function __get($name)
  {
    if ('code' == $name) {
      return $this->$name;
    } else {
      return false;
    }
  }
 
  /**
   * 生成code
   */
  protected function createCode()
  {
    switch ($this->codeType) {
      case 0:
        $code = $this->getNum();
        break;
      case 1:
        $code = $this->getChar();
        break;
      case 2:
        $code = $this->getNumChar();
        break;
      default:
        die('样式不对');
    }
    return $code;
  }
 
  /**
   * 数字验证码
   * @return string
   */
  protected function getNum()
  {
    $str = join('', range(0,9));
    return substr(str_shuffle($str), 0, $this->number);
  }
 
  /**
   * 字符验证码
   * @return string
   */
  protected function getChar()
  {
    $str = join('', range('a', 'z'));
    $str = $str . strtoupper($str);
    return substr(str_shuffle($str), 0, $this->number);
  }
 
  /**
   * 字符和数字混合验证码
   * @return string
   */
  protected function getNumChar()
  {
    $num = join('', range(0, 9));
    $str = join('', range('a', 'z'));
    $str_big = strtoupper($str);
    $numChar = $num . $str . $str_big;
    return substr(str_shuffle($numChar), 0, $this->number);
  }
 
  /**
   * 生成图像
   */
  protected function createImage()
  {
    $this->image = imagecreatetruecolor($this->width, $this->height);
  }
 
  /**
   * 填充背景色
   */
  protected function fillColor()
  {
    imagefill($this->image, 0, 0, $this->lightColor());
  }
 
  /**
   * 浅颜色
   * @return int
   */
  protected function lightColor()
  {
    return imagecolorallocate($this->image, mt_rand(170, 255), mt_rand(170, 255), mt_rand(170, 255));
  }
 
  /**
   * 深颜色
   * @return int
   */
  protected function darkColor()
  {
    return imagecolorallocate($this->image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
  }
 
  /**
   * 添加验证码字符
   */
  protected function drawChar()
  {
    $width = ceil($this->width/$this->number);
    for ($i = 0; $i < $this->number; $i++) {
      $x = mt_rand($i * ($width - 5), ($i + 1) * ($width - 5));
      $y = mt_rand(0, $this->height - 15);
      imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
    }
  }
 
  /**
   * 添加干扰点
   */
  protected function drawDisturb()
  {
    for ($i= 0; $i < 100; $i++) {
      imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $this->darkColor());
    }
  }
 
  /**
   * 添加干扰线
   */
  protected function drawArc()
  {
    for ($i = 0; $i < $this->number - 3; $i++) {
      imagearc($this->image, mt_rand(5, $this->width), mt_rand(5, $this->height), mt_rand(5, $this->width), mt_rand(5, $this->height),mt_rand(0, 70), mt_rand(300, 360), $this->darkColor());
    }
  }
 
  /**
   * 输出显示
   */
  protected function show()
  {
    header('Content-Type:image/png');
    imagepng($this->image);
  }
 
  /**
   * 外部image
   */
  public function outImage()
  {
    $this->createImage();//创建画布
    $this->fillColor();//填充背景色
    $this->drawChar();//添加验证字符
    $this->drawDisturb();//添加干扰点
    $this->drawArc();//添加干扰线
    $this->show();//输出
  }
}

展示验证码。。保存验证码和过期时间

<?php
include './code/Code.php';
 
$code = new code\Code();
$code->outImage();
session_start();
$_SESSION['code'] = [
  'code' => $code->code,
  'exp_time' => time() + (60 * 60 * 10),
];

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
第四节--构造函数和析构函数
Nov 16 PHP
PHP通过COM使用ADODB的简单例子
Dec 31 PHP
PHP连接局域网MYSQL数据库的简单实例
Aug 26 PHP
PHP的MVC模式实现原理分析(一相简单的MVC框架范例)
Apr 29 PHP
php 类自动载入的方法
Jun 03 PHP
php数组比较实现查找连续数的方法
Jul 29 PHP
对比PHP对MySQL的缓冲查询和无缓冲查询
Jul 01 PHP
Yii2针对指定url的生成及图片等的引入方法小结
Jul 18 PHP
php mysql_real_escape_string addslashes及mysql绑定参数防SQL注入攻击
Dec 23 PHP
PHP框架Laravel中使用UUID实现数据分表操作示例
May 30 PHP
Laravel 实现Controller向blade前台模板赋值的四种方式小结
Oct 22 PHP
php如何获取Http请求
Apr 30 PHP
PHP 计算至少是其他数字两倍的最大数的实现代码
May 26 #PHP
tp5.1 框架数据库-数据集操作实例分析
May 26 #PHP
tp5.1 框架路由操作-URL生成实例分析
May 26 #PHP
tp5.1 框架join方法用法实例分析
May 26 #PHP
tp5.1框架数据库子查询操作实例分析
May 26 #PHP
tp5.1 框架数据库常见操作详解【添加、删除、更新、查询】
May 26 #PHP
Laravel 修改验证异常的响应格式实例代码详解
May 25 #PHP
You might like
风味层面去分析咖啡油脂
2021/03/03 咖啡文化
mysql limit查询优化分析
2008/11/12 PHP
php验证手机号码(支持归属地查询及编码为UTF8)
2013/02/01 PHP
php实现将二维关联数组转换成字符串的方法详解
2017/07/31 PHP
PHP一致性hash分布式算法封装类定义与用法示例
2018/08/04 PHP
JavaScript的eval JSON object问题
2009/11/15 Javascript
jQuery Lightbox 图片展示插件使用说明
2010/04/25 Javascript
jquery ztree实现下拉树形框使用到了json数据
2014/05/14 Javascript
jQuery控制的不同方向的滑动(向左、向右滑动等)
2014/07/18 Javascript
JQuery中上下文选择器实现方法
2015/05/18 Javascript
JavaScript必知必会(二) null 和undefined
2016/06/08 Javascript
分享javascript、jquery实用代码段
2016/10/20 Javascript
Angularjs中ng-repeat的简单实例
2017/08/25 Javascript
浅谈Vue.js路由管理器 Vue Router
2018/08/16 Javascript
React 使用recharts实现散点地图的示例代码
2018/12/07 Javascript
Vue实现push数组并删除的例子
2019/11/01 Javascript
区分vue-router的hash和history模式
2020/10/03 Javascript
python进程管理工具supervisor使用实例
2014/09/17 Python
python查找指定具有相同内容文件的方法
2015/06/28 Python
python调用API实现智能回复机器人
2018/04/10 Python
python 除法保留两位小数点的方法
2018/07/16 Python
详解django的serializer序列化model几种方法
2018/10/16 Python
利用ctypes获取numpy数组的指针方法
2019/02/12 Python
Python通过TensorFLow进行线性模型训练原理与实现方法详解
2020/01/15 Python
css3利用transform变形结合事件完成扇形导航
2020/10/26 HTML / CSS
英国最大的汽车交易网站:Auto Trader UK
2016/09/23 全球购物
c++工程师面试问题
2013/08/04 面试题
上海微创软件面试题
2012/06/14 面试题
采购主管岗位职责
2014/02/01 职场文书
企业安全生产目标责任书
2014/07/23 职场文书
自主招生学校推荐信
2014/09/26 职场文书
公安局副政委班子个人对照检查材料
2014/10/04 职场文书
教师批评与自我批评总结
2014/10/16 职场文书
怎样写辞职信
2015/02/27 职场文书
雷锋的观后感
2015/06/10 职场文书
2019年图书室自查报告范本
2019/10/12 职场文书