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 相关文章推荐
php获取某个目录大小的代码
Sep 10 PHP
WordPress判断用户是否登录的代码
Mar 17 PHP
PHP array操作10个小技巧分享
Jun 23 PHP
php中批量替换文件名的实现代码
Jul 20 PHP
PHP Error与Logging函数的深入理解
Jun 03 PHP
php中使用gd库实现远程图片下载实例
May 12 PHP
php排序算法实例分析
Oct 17 PHP
php中遍历二维数组并以表格的形式输出的方法
Jan 03 PHP
php基于自定义函数记录log日志方法
Jul 21 PHP
PHP延迟静态绑定的深入讲解
Apr 02 PHP
Laravel重定向,a链接跳转,控制器跳转示例
Oct 22 PHP
详解php中流行的rpc框架
May 29 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
实例(Smarty+FCKeditor新闻系统)
2007/01/02 PHP
PHP 图片水印类代码
2012/08/27 PHP
初识php MVC
2014/09/10 PHP
PHP的Laravel框架结合MySQL与Redis数据库的使用部署
2016/03/21 PHP
ThinkPHP打水印及设置水印位置的方法
2016/10/14 PHP
PHP连接sftp并下载文件的方法教程
2018/08/26 PHP
让Laravel API永远返回JSON格式响应的方法示例
2018/09/05 PHP
谈谈JavaScript中的函数与闭包
2013/04/14 Javascript
jQuery源码分析之jQuery.fn.each与jQuery.each用法
2015/01/23 Javascript
纯javascript实现图片延时加载方法
2015/08/21 Javascript
Bootstrap模块dropdown实现下拉框响应
2016/05/22 Javascript
js代码实现下拉菜单【推荐】
2016/12/15 Javascript
js 转义字符及URI编码详解
2017/02/28 Javascript
nodejs开发——express路由与中间件
2017/03/24 NodeJs
JS实现标签页切换效果
2017/05/04 Javascript
JQuery实现定时刷新功能代码
2017/05/09 jQuery
Vue列表页渲染优化详解
2017/07/24 Javascript
vue中使用refs定位dom出现undefined的解决方法
2017/12/21 Javascript
vue2中使用sass并配置全局的sass样式变量的方法
2018/09/04 Javascript
JavaScript JMap类定义与使用方法示例
2019/01/22 Javascript
微信小程序Page中data数据操作和函数调用方法
2019/05/08 Javascript
vuex actions异步修改状态的实例详解
2019/11/06 Javascript
Vue实现多页签组件
2021/01/14 Vue.js
[16:19]教你分分钟做大人——风暴之灵
2015/03/11 DOTA
[03:15]DOTA2-DPC中国联赛1月22日Recap集锦
2021/03/11 DOTA
python中sets模块的用法实例
2014/09/30 Python
python实现windows下文件备份脚本
2018/05/27 Python
Python数据抓取爬虫代理防封IP方法
2018/12/23 Python
Python修改文件往指定行插入内容的实例
2019/01/30 Python
Python3变量与基本数据类型用法实例分析
2020/02/14 Python
python 星号(*)的多种用途
2020/09/21 Python
司机辞职报告范文
2014/01/20 职场文书
物业管理专业求职信
2014/06/11 职场文书
高中课前三分钟演讲稿
2014/08/18 职场文书
聘任证明怎么写
2015/03/02 职场文书
浅谈redis五大数据结构和使用场景
2021/04/12 Redis