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 相关文章推荐
建立动态的WML站点(三)
Oct 09 PHP
PHP面向对象分析设计的61条军规小结
Jul 17 PHP
PHP filter_var() 函数 Filter 函数
Apr 25 PHP
PHP中根据IP地址判断城市实现城市切换或跳转代码
Sep 04 PHP
PHP获取短链接跳转后的真实地址和响应头信息的方法
Jul 25 PHP
php常用数学函数汇总
Nov 21 PHP
PHP中if和or运行效率对比
Dec 12 PHP
Yii中srbac权限扩展模块工作原理与用法分析
Jul 14 PHP
CI框架实现框架前后端分离的方法详解
Dec 30 PHP
mac os快速切换多个PHP版本的方法
Mar 07 PHP
php-app开发接口加密详解
Apr 18 PHP
php字符串函数 str类常见用法示例
May 15 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表单提交后引号前自动加反斜杠的原因及三种办法关闭php魔术引号
2015/09/30 PHP
phalcon model在插入或更新时会自动验证非空字段的解决办法
2016/12/29 PHP
php mysql实现mysql_select_db选择数据库
2016/12/30 PHP
php+ajax实现文件切割上传功能示例
2020/03/03 PHP
javascript动态改变img的src属性图片不显示的解决方法
2010/10/20 Javascript
JS 获取浏览器和屏幕宽高等信息的实现思路及代码
2013/07/31 Javascript
一个简单的jquery的多选下拉框(自写)
2014/05/05 Javascript
JavaScript基于setTimeout实现计数的方法
2015/05/08 Javascript
深入解析桶排序算法及Node.js上JavaScript的代码实现
2016/07/06 Javascript
4个顶级JavaScript高级文本编辑器
2018/10/10 Javascript
Vue.js 中的 v-cloak 指令及使用详解
2018/11/19 Javascript
[01:10:16]DOTA2上海特级锦标赛B组资格赛#2 Fnatic VS Spirit第一局
2016/02/27 DOTA
[03:11]TI9战队档案 - Alliance
2019/08/20 DOTA
探寻python多线程ctrl+c退出问题解决方案
2014/10/23 Python
python正则表达式match和search用法实例
2015/03/26 Python
初步解析Python中的yield函数的用法
2015/04/03 Python
安装Python的web.py框架并从hello world开始编程
2015/04/25 Python
jupyter安装小结
2016/03/13 Python
python 调用c语言函数的方法
2017/09/29 Python
对python中的乘法dot和对应分量相乘multiply详解
2018/11/14 Python
flask session组件的使用示例
2018/12/25 Python
Python进阶之使用selenium爬取淘宝商品信息功能示例
2019/09/16 Python
python实现简单俄罗斯方块
2020/03/13 Python
Python pip安装模块提示错误解决方案
2020/05/22 Python
python实现MySQL指定表增量同步数据到clickhouse的脚本
2021/02/26 Python
Bowflex美国官方网站:高级家庭健身器材
2017/12/22 全球购物
简述Linux文件系统通过i节点把文件的逻辑结构和物理结构转换的工作过程
2016/01/06 面试题
土木工程实习生自我鉴定
2013/09/19 职场文书
预备党员党校学习自我评价分享
2013/11/12 职场文书
英文简历自荐信范文
2013/12/11 职场文书
师范学院美术系毕业生自我鉴定
2014/01/29 职场文书
学生手册评语
2014/05/05 职场文书
主要负责人任命书
2014/06/06 职场文书
给客户的感谢信
2015/01/21 职场文书
详解TS数字分隔符和更严格的类属性检查
2021/05/06 Javascript
python缺失值填充方法示例代码
2022/12/24 Python