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 相关文章推荐
聊天室php&amp;mysql(三)
Oct 09 PHP
PHP实现图片简单上传
Oct 09 PHP
PHP 远程文件管理,可以给表格排序,遍历目录,时间排序
Aug 07 PHP
无法载入 mcrypt 扩展,请检查 PHP 配置终极解决方案
Jul 18 PHP
基于php常用函数总结(数组,字符串,时间,文件操作)
Jun 27 PHP
php模拟post提交数据的方法
Feb 12 PHP
php使用pear_smtp发送邮件
Apr 15 PHP
PHP 常用时间函数资料整理
Oct 22 PHP
Yii2中添加全局函数的方法分析
May 04 PHP
iis 7下安装laravel 5.4环境的方法教程
Jun 14 PHP
PHP高效获取远程图片尺寸和大小的实现方法
Oct 20 PHP
TP5框架model常见操作示例小结【增删改查、聚合、时间戳、软删除等】
Apr 05 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
《PHP编程最快明白》第二讲 数字、浮点、布尔型、字符串和数组
2010/11/01 PHP
深入解析PHP垃圾回收机制对内存泄露的处理
2013/06/14 PHP
php函数实现判断是否移动端访问
2015/03/03 PHP
laravel创建类似ThinPHP中functions.php的全局函数
2016/11/26 PHP
php使用parse_str实现查询字符串解析到变量中的方法
2017/02/17 PHP
visual studio code 调试php方法(图文详解)
2017/09/15 PHP
php5与php7的区别点总结
2019/10/11 PHP
你可能不再需要JQUERY
2021/03/09 Javascript
[原创]保存的js无法执行的解决办法
2007/02/25 Javascript
Ext javascript建立超链接,进行事件处理的实现方法
2009/03/22 Javascript
JavaScript DOM事件(笔记)
2015/04/08 Javascript
js重写方法的简单实现
2016/07/10 Javascript
Bootstrap常用组件学习(整理)
2017/03/24 Javascript
使用JavaScript实现alert的实例代码
2017/07/06 Javascript
vue引入swiper插件的使用实例
2017/07/19 Javascript
使用Vue开发一个实时性时间转换指令
2018/01/17 Javascript
解决vue-cli webpack打包后加载资源的路径问题
2018/09/25 Javascript
elementUI 设置input的只读或禁用的方法
2018/10/30 Javascript
在webstorm开发微信小程序之使用阿里自定义字体图标的方法
2018/11/15 Javascript
elementUI select组件value值注意事项详解
2019/05/29 Javascript
解决vue初始化项目时,一直卡在Project description上的问题
2019/10/31 Javascript
es6函数之rest参数用法实例分析
2020/04/18 Javascript
[02:32]DOTA2英雄基础教程 美杜莎
2014/01/07 DOTA
用Python将结果保存为xlsx的方法
2019/01/28 Python
浅谈Pandas Series 和 Numpy array中的相同点
2019/06/28 Python
Python中的 sort 和 sorted的用法与区别
2019/08/10 Python
python文件操作seek()偏移量,读取指正到指定位置操作
2020/07/05 Python
Python可视化工具如何实现动态图表
2020/10/23 Python
HTML5中的Article和Section元素认识及使用
2013/03/22 HTML / CSS
【HTML5】3D模型--百行代码实现旋转立体魔方实例
2016/12/16 HTML / CSS
戴森美国官网:Dyson美国
2016/09/11 全球购物
应聘编辑自荐信范文
2014/03/12 职场文书
财务担保书范文
2014/04/02 职场文书
奥巴马英文演讲稿
2014/05/15 职场文书
银行优秀员工事迹材料
2014/05/29 职场文书
实训报告范文大全
2014/11/04 职场文书