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聊天室技术
Oct 09 PHP
php中利用post传递字符串重定向的实现代码
Apr 21 PHP
php函数array_merge用法一例(合并同类数组)
Feb 03 PHP
PHP中图片等比缩放的实例
Mar 24 PHP
基于php无限分类的深入理解
Jun 02 PHP
PHP $_FILES中error返回值详解
Jan 30 PHP
php反射应用示例
Feb 25 PHP
php使用parse_url和parse_str解析URL
Feb 22 PHP
php解析base64数据生成图片的方法
Dec 06 PHP
什么是PHP文件?如何打开PHP文件?
Jun 27 PHP
thinkPHP多表查询及分页功能实现方法示例
Jul 03 PHP
laravel实现前后台路由分离的方法
Oct 13 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定时计划任务的实现方法详解
2013/06/06 PHP
PHP按指定键值对二维数组进行排序的方法
2015/12/22 PHP
php通过淘宝API查询IP地址归属等信息
2015/12/25 PHP
PHP数学运算函数大汇总(经典值得收藏)
2016/04/01 PHP
PHP怎样用正则抓取页面中的网址
2016/08/09 PHP
php插件Xajax使用方法详解
2017/08/31 PHP
PHP实现动态添加XML中数据的方法
2018/03/30 PHP
js特殊字符转义介绍
2013/11/05 Javascript
node-http-proxy修改响应结果实例代码
2016/06/06 Javascript
Bootstrap实现的标签页内容切换显示效果示例
2017/05/25 Javascript
微信小程序支付PHP代码
2018/08/23 Javascript
vue-cli项目代理proxyTable配置exclude的方法
2018/09/20 Javascript
Vuex实现数据增加和删除功能
2019/11/11 Javascript
js实现盒子移动动画效果
2020/08/09 Javascript
jquery实现简易验证插件封装
2020/09/13 jQuery
python实现ip查询示例
2014/03/26 Python
python获取元素在数组中索引号的方法
2015/07/15 Python
eclipse创建python项目步骤详解
2019/05/10 Python
python GUI库图形界面开发之PyQt5布局控件QVBoxLayout详细使用方法与实例
2020/03/06 Python
python DES加密与解密及hex输出和bs64格式输出的实现代码
2020/04/13 Python
使用Canvas操作像素的方法
2018/06/14 HTML / CSS
实例教程 HTML5 Canvas 超炫酷烟花绽放动画实现代码
2014/11/05 HTML / CSS
世界上最大的乐器零售商:Guitar Center
2017/11/07 全球购物
PacSun官网:加州生活方式服装、鞋子和配饰
2018/03/10 全球购物
RealTek面试题
2016/06/28 面试题
应届生的求职推荐信范文
2013/11/30 职场文书
列车长先进事迹材料
2014/01/25 职场文书
餐厅总厨求职信
2014/03/04 职场文书
实用的简历自我评价
2014/03/06 职场文书
商超业务员岗位职责
2014/03/12 职场文书
七匹狼男装广告词
2014/03/21 职场文书
英语课前三分钟演讲稿(6篇)
2014/09/13 职场文书
个人诉讼委托书范本
2014/10/17 职场文书
外贸采购员岗位职责
2015/04/03 职场文书
身份证丢失证明
2015/06/19 职场文书
高二数学教学反思
2016/02/18 职场文书