Laravel下生成验证码的类


Posted in PHP onNovember 15, 2017

本文实例为大家分享了Laravel生成验证码的类,供大家参考,具体内容如下

<?php
 
namespace App\Tool\Validate;
 
//验证码类
class ValidateCode {
  private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
  private $code;//验证码
  private $codelen = 4;//验证码长度
  private $width = 130;//宽度
  private $height = 50;//高度
  private $img;//图形资源句柄
  private $font;//指定的字体
  private $fontsize = 20;//指定字体大小
  private $fontcolor;//指定字体颜色
 
  //构造方法初始化
  public function __construct()
  {
    $this->font = public_path() . '/fonts/Elephant.ttf';//注意字体路径要写对,否则显示不了图片
    $this->createCode();
  }
  //生成随机码
  private function createCode()
  {
    $_len = strlen($this->charset) - 1;
    for ($i = 0;$i < $this->codelen;++$i) {
      $this->code .= $this->charset[mt_rand(0, $_len)];
    }
  }
  //生成背景
  private function createBg()
  {
    $this->img = imagecreatetruecolor($this->width, $this->height);
    $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
    imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
  }
  //生成文字
  private function createFont()
  {
    $_x = $this->width / $this->codelen;
    for ($i = 0;$i < $this->codelen;++$i) {
      $this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
    }
  }
  //生成线条、雪花
  private function createLine()
  {
    //线条
    for ($i = 0;$i < 6;++$i) {
      $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
      imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
    }
    //雪花
    for ($i = 0;$i < 100;++$i) {
      $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
      imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
    }
  }
  //输出
  private function outPut()
  {
    header('Content-type:image/png');
    imagepng($this->img);
    imagedestroy($this->img);
  }
  //对外生成
  public function doimg()
  {
    $this->createBg();
    $this->createLine();
    $this->createFont();
    $this->outPut();
  }
  //获取验证码
  public function getCode()
  {
    return strtolower($this->code);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
杏林同学录(二)
Oct 09 PHP
php5.3 goto函数介绍和示例
Mar 21 PHP
PHP实现无限极分类图文教程
Nov 25 PHP
PHP中的traits实现代码复用使用实例
May 13 PHP
php创建无限级树型菜单
Nov 05 PHP
ThinkPHP中limit()使用方法详解
Apr 19 PHP
PHP使用DOM和simplexml读取xml文档的方法示例
Feb 08 PHP
Kindeditor编辑器添加图片上传水印功能(php代码)
Aug 03 PHP
PHP中的访问修饰符简单比较
Feb 02 PHP
PHP fopen函数用法实例讲解
Feb 15 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
Feb 16 PHP
phpmyadmin在宝塔面板里进不去的解决方案
Jul 06 PHP
Ajax中的JSON格式与php传输过程全面解析
Nov 14 #PHP
PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
Nov 14 #PHP
Laravel Intervention/image图片处理扩展包的安装、使用与可能遇到的坑详解
Nov 14 #PHP
PHP中递归的实现实例详解
Nov 14 #PHP
利用Homestead快速运行一个Laravel项目的方法详解
Nov 14 #PHP
PHP对称加密算法(DES/AES)类的实现代码
Nov 14 #PHP
浅谈PHP中如何实现Hook机制
Nov 14 #PHP
You might like
PHP实现采集程序原理和简单示例代码
2007/03/18 PHP
php读取html并截取字符串的简单代码
2009/11/30 PHP
PHP5.5.15+Apache2.4.10+MySQL5.6.20配置方法分享
2016/05/06 PHP
php注册系统和使用Xajax即时验证用户名是否被占用
2017/08/31 PHP
Javascript 强制类型转换函数
2009/05/17 Javascript
JavaScript 序列化对象实现代码
2009/12/18 Javascript
js和as的稳定传值问题解决
2013/07/14 Javascript
javascript实现存储hmtl字符串示例
2014/04/25 Javascript
jquery实现右键菜单插件
2015/03/29 Javascript
js与jQuery实现checkbox复选框全选/全不选的方法
2016/01/05 Javascript
JavaScript数组的定义及数字操作技巧
2016/06/06 Javascript
Node.js中.pfx后缀文件的处理方法
2017/03/10 Javascript
Angular.JS利用ng-disabled属性和ng-model实现禁用button效果
2017/04/05 Javascript
ES6中新增的Object.assign()方法详解
2017/09/22 Javascript
使用elementUI实现将图片上传到本地的示例
2018/09/04 Javascript
vscode 开发Vue项目的方法步骤
2018/11/25 Javascript
JavaScript实现HTML导航栏下拉菜单
2020/11/25 Javascript
python3使用urllib示例取googletranslate(谷歌翻译)
2014/01/23 Python
python实现excel读写数据
2021/03/02 Python
Python读取数据集并消除数据中的空行方法
2018/07/12 Python
对Python正则匹配IP、Url、Mail的方法详解
2018/12/25 Python
Python装饰器用法实例分析
2019/01/14 Python
Python 3.6 -win64环境安装PIL模块的教程
2019/06/20 Python
Pycharm新手使用教程(图文详解)
2020/09/17 Python
在IE6系列等老式浏览器中使用HTML5的新标签实现方案
2012/12/25 HTML / CSS
RentCars.com巴西:汽车租赁网站
2016/08/22 全球购物
瑰珀翠美国官网:Crabtree & Evelyn美国
2016/11/29 全球购物
ghd官网:英国ghd直发器品牌
2018/05/04 全球购物
同居协议书范本
2014/04/23 职场文书
个人融资协议书范本两则
2014/10/15 职场文书
2014年小学数学教师工作总结
2014/12/03 职场文书
计算机专业自荐信范文
2015/03/26 职场文书
师德师风培训感言
2015/08/03 职场文书
餐厅服务员管理制度
2015/08/05 职场文书
小公司融资,商业计划书的8切记
2019/07/15 职场文书
实现AJAX异步调用和局部刷新的基本步骤
2022/03/17 Javascript