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数组实现无限分类,不使用数据库,不使用递归.
Dec 09 PHP
php中判断字符串是否全是中文或含有中文的实现代码
Sep 16 PHP
提高php运行速度的一些小技巧分享
Jul 03 PHP
PHP批量采集下载美女图片的实现代码
Jun 03 PHP
兼容各大浏览器带关闭按钮的漂浮多组图片广告代码
Jun 05 PHP
PHP将session信息存储到数据库的类实例
Mar 04 PHP
Ecshop 后台添加新功能栏目及管理权限设置教程
Nov 21 PHP
PHP使用PhpSpreadsheet操作Excel实例详解
Mar 26 PHP
PHP Web表单生成器案例分析
Jun 02 PHP
ThinkPHP5分页paginate代码实例解析
Nov 10 PHP
用php实现分页效果的示例代码
Dec 10 PHP
php中配置文件保存修改操作 如config.php文件的读取修改等操作
May 12 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工厂模式的好处
2013/06/18 PHP
php设计模式之适配器模式实例分析【星际争霸游戏案例】
2020/04/07 PHP
优化网页之快速的呈现我们的网页
2007/06/29 Javascript
javascript new fun的执行过程
2010/08/05 Javascript
jQuery EasyUI API 中文文档 - Tree树使用介绍
2011/11/19 Javascript
深入理解JavaScript系列(6):S.O.L.I.D五大原则之单一职责SRP
2012/01/15 Javascript
js单向链表的具体实现实例
2013/06/21 Javascript
Vue.js常用指令汇总(v-if、v-for等)
2016/11/03 Javascript
angularJS 指令封装回到顶部示例详解
2017/01/22 Javascript
微信小程序 页面跳转事件绑定的实例详解
2017/09/20 Javascript
微信小程序如何获取用户手机号
2018/01/26 Javascript
vue插件实现v-model功能
2018/09/10 Javascript
vue实现循环切换动画
2018/10/17 Javascript
Web安全之XSS攻击与防御小结
2018/12/13 Javascript
基于js实现抽红包并分配代码实例
2019/09/19 Javascript
numpy ndarray 取出满足特定条件的某些行实例
2019/12/05 Python
解决python3插入mysql时内容带有引号的问题
2020/03/02 Python
Python SMTP配置参数并发送邮件
2020/06/16 Python
Flask中sqlalchemy模块的实例用法
2020/08/02 Python
HTML5实现音频和视频嵌入的方法
2018/08/22 HTML / CSS
中国领先的专业家电网购平台:国美在线
2016/12/25 全球购物
教师自荐书
2013/10/08 职场文书
八年级历史教学反思
2014/01/10 职场文书
工艺员岗位职责
2014/02/11 职场文书
函授生自我鉴定
2014/03/25 职场文书
解除财产保全担保书
2014/05/20 职场文书
医学专业毕业生推荐信
2014/07/12 职场文书
2014年学习部工作总结
2014/11/12 职场文书
劳动仲裁撤诉申请书
2015/05/18 职场文书
2015年外贸业务员工作总结范文
2015/05/23 职场文书
优秀共产党员事迹材料2016
2016/02/29 职场文书
如何使用JavaScript策略模式校验表单
2021/04/29 Javascript
Ajax 的初步实现(使用vscode+node.js+express框架)
2021/06/18 Javascript
Python使用华为API为图像设置多个锚点标签
2022/04/12 Python
MySQL存储过程及语法详解
2022/08/05 MySQL
HTML页面点击按钮关闭页面的多种方式
2022/12/24 HTML / CSS