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 相关文章推荐
不用GD库生成当前时间的PNG格式图象的程序
Oct 09 PHP
PHP4.04简明安装
Oct 09 PHP
PHP面向对象分析设计的经验原则
Sep 20 PHP
php str_pad() 将字符串填充成指定长度的字符串
Feb 23 PHP
PHP网站备份程序代码分享
Jun 10 PHP
php中http_build_query 的一个问题
Mar 25 PHP
ThinkPHP CURD方法之order方法详解
Jun 18 PHP
php安装swoole扩展的方法
Mar 19 PHP
PHP封装的Twitter访问类实例
Jul 18 PHP
php图片水印添加、压缩、剪切的封装类实现
Apr 18 PHP
php smtp实现发送邮件功能
Jun 22 PHP
PHP fclose函数用法总结
Feb 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 stripslashes和addslashes的区别
2014/02/03 PHP
php中smarty实现多模版网站的方法
2015/06/11 PHP
php unlink()函数使用教程
2018/07/12 PHP
laravel实现查询最后执行的一条sql语句的方法
2019/10/09 PHP
jquery 防止表单重复提交代码
2010/01/21 Javascript
JQuery模板插件 jquery.tmpl 动态ajax扩展
2011/11/10 Javascript
jquery创建并行对象或者合并对象的实现代码
2012/10/10 Javascript
js分页工具实例
2015/01/28 Javascript
详解JavaScript的AngularJS框架中的作用域与数据绑定
2016/03/04 Javascript
BootStrap智能表单实战系列(十一)级联下拉的支持
2016/06/13 Javascript
详解vue 中使用 AJAX获取数据的方法
2017/01/18 Javascript
jQuery事件对象的属性和方法详解
2017/09/09 jQuery
angular指令笔记ng-options的使用方法
2017/09/18 Javascript
vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀
2018/06/13 Javascript
小程序分页实践之编写可复用分页组件
2019/07/18 Javascript
vue 使用鼠标滚动加载数据的例子
2019/10/31 Javascript
Vue多选列表组件深入详解
2021/03/02 Vue.js
Python中的装饰器用法详解
2015/01/14 Python
python查看微信好友是否删除自己
2016/12/19 Python
解决Python3 被PHP程序调用执行返回乱码的问题
2019/02/16 Python
对Python强大的可变参数传递机制详解
2019/06/13 Python
PyQt5响应回车事件的方法
2019/06/25 Python
Python:合并两个numpy矩阵的实现
2019/12/02 Python
Matplotlib绘制雷达图和三维图的示例代码
2020/01/07 Python
python实现word文档批量转成自定义格式的excel文档的思路及实例代码
2020/02/21 Python
Python实时监控网站浏览记录实现过程详解
2020/07/14 Python
解决导入django_filters不成功问题No module named 'django_filter'
2020/07/15 Python
PyQt中使用QtSql连接MySql数据库的方法
2020/07/28 Python
澳大利亚潮流尖端的快时尚品牌:Cotton On
2016/09/26 全球购物
阿迪达斯印度官方商城:adidas India
2017/03/26 全球购物
自荐信怎么写呢?
2013/12/09 职场文书
文秘档案管理岗位职责
2014/03/06 职场文书
元旦文艺汇演主持词
2014/03/26 职场文书
和领导吃饭祝酒词
2015/08/11 职场文书
某某店铺的开业庆典主持词范本
2019/11/25 职场文书
实习员工转正的评语汇总,以备不时之需
2019/12/17 职场文书