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图片上传程序
Mar 27 PHP
处理单名多值表单的详解
Jun 08 PHP
PHP页面中文乱码分析
Oct 29 PHP
php解决抢购秒杀抽奖等大流量并发入库导致的库存负数的问题
Jun 19 PHP
PHP中魔术变量__METHOD__与__FUNCTION__的区别
Sep 29 PHP
php数组添加元素方法小结
Dec 20 PHP
php curl登陆qq后获取用户信息时证书错误
Feb 03 PHP
腾讯CMEM的PHP扩展编译安装方法
Sep 25 PHP
用PHP将Unicode 转化为UTF-8的实现方法(推荐)
Feb 08 PHP
在Mac OS下搭建LNMP开发环境的步骤详解
Mar 10 PHP
php设计模式之原型模式分析【星际争霸游戏案例】
Mar 23 PHP
Laravel统一错误处理为JSON的方法介绍
Oct 18 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下连接mssql2005的代码
2011/01/17 PHP
PHP写UltraEdit插件脚本实现方法
2011/12/26 PHP
PHP常用开发函数解析之数组篇[未完结]
2012/07/30 PHP
浅谈PHP接收POST数据方式
2015/06/05 PHP
php观察者模式应用场景实例详解
2017/02/03 PHP
PHP 计算两个特别大的整数实例代码
2018/05/07 PHP
tp5框架前台无限极导航菜单类实现方法分析
2020/03/29 PHP
jQuery textarea的长度进行验证
2009/05/06 Javascript
jquery里的each使用方法详解
2010/12/22 Javascript
使用javascript实现ListBox左右全选,单选,多选,全请
2013/11/07 Javascript
JavaScript中获取鼠标位置相关属性总结
2014/10/11 Javascript
jQuery对象的selector属性用法实例
2014/12/27 Javascript
Angular中的Promise对象($q介绍)
2015/03/03 Javascript
javascript制作的简单注册模块表单验证
2015/04/13 Javascript
简介JavaScript中fixed()方法的使用
2015/06/08 Javascript
jquery使整个div区域可以点击的方法
2015/06/24 Javascript
jQuery validate插件功能与用法详解
2016/12/15 Javascript
nodejs超出最大的调用栈错误问题
2017/12/27 NodeJs
NodeJS简单实现WebSocket功能示例
2018/02/10 NodeJs
mpvue写一个CPASS小程序的示例
2018/09/04 Javascript
详细教你微信公众号正文页SVG交互开发技巧
2019/07/25 Javascript
vue + el-form 实现的多层循环表单验证
2020/11/25 Vue.js
[01:01]2020完美高校联赛(秋)西安落幕
2021/03/11 DOTA
利用Python实现简单的相似图片搜索的教程
2015/04/23 Python
Python素数检测实例分析
2015/06/15 Python
python async with和async for的使用
2019/06/20 Python
python保留格式汇总各部门excel内容的实现思路
2020/06/01 Python
FitFlop美国官网:英国符合人体工学的鞋类品牌
2018/10/05 全球购物
职工运动会邀请函
2014/01/19 职场文书
小学生秋游活动方案
2014/02/23 职场文书
考核工作实施方案
2014/03/30 职场文书
一份文言文检讨书
2014/09/13 职场文书
西岭雪山导游词
2015/02/06 职场文书
2015年房地产销售工作总结
2015/04/20 职场文书
2015年乡镇纪委工作总结
2015/05/26 职场文书
MySql数据库触发器使用教程
2022/06/01 MySQL