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 相关文章推荐
使用sockets:从新闻组中获取文章(三)
Oct 09 PHP
URL Rewrite的设置方法
Jan 02 PHP
PHP配置文件中最常用四个ini函数
Mar 19 PHP
php adodb操作mysql数据库
Mar 19 PHP
简单的php写入数据库类代码分享
Jul 26 PHP
php中的注释、变量、数组、常量、函数应用介绍
Nov 16 PHP
浅析php原型模式
Nov 25 PHP
Zend Framework分页类用法详解
Mar 22 PHP
php自定义函数实现JS的escape的方法示例
Jul 07 PHP
PHP获取文本框、密码域、按钮的值实例代码
Apr 19 PHP
laravel5.4利用163邮箱发送邮件的步骤详解
Sep 22 PHP
PHP实现对数字分隔加千分号的方法
Mar 18 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 页面执行时间计算代码
2008/12/04 PHP
php运行出现Call to undefined function curl_init()的解决方法
2010/11/02 PHP
整理php防注入和XSS攻击通用过滤
2015/09/13 PHP
laravel withCount 统计关联数量的方法
2019/10/10 PHP
JavaScript修改css样式style
2008/04/15 Javascript
JavaScript Event学习第十章 一些可替换的事件对
2010/02/10 Javascript
jQuery1.6 使用方法一
2011/11/23 Javascript
2014 HTML5/CSS3热门动画特效TOP10
2014/12/07 Javascript
js中this的用法实例分析
2015/01/10 Javascript
JS实现从顶部下拉显示的带动画QQ客服特效代码
2015/10/24 Javascript
js字符串与Unicode编码互相转换
2017/05/17 Javascript
JavaScript实现跟随滚动缓冲运动广告框
2017/07/15 Javascript
微信小程序 如何引入外部字体库iconfont的图标
2018/01/31 Javascript
JavaScript折半查找(二分查找)算法原理与实现方法示例
2018/08/06 Javascript
详解用async/await来处理异步
2019/08/28 Javascript
详细分析Node.js 多进程
2020/06/22 Javascript
[01:18:33]Secret vs VGJ.S Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
详解Python验证码识别
2016/01/25 Python
python安装Scrapy图文教程
2017/08/14 Python
Python实现返回数组中第i小元素的方法示例
2017/12/04 Python
python 中的list和array的不同之处及转换问题
2018/03/13 Python
使用 Python 处理 JSON 格式的数据
2019/07/22 Python
Python使用pickle进行序列化和反序列化的示例代码
2020/09/22 Python
华为旗下电子商务平台:华为商城
2016/08/06 全球购物
美国殿堂级滑板、冲浪、滑雪服装品牌:Volcom(钻石)
2017/04/20 全球购物
The North Face北面荷兰官网:美国著名户外品牌
2019/10/16 全球购物
英国拖鞋购买网站:Bedroom Athletics
2020/02/28 全球购物
意大利领先的奢侈品在线时装零售商:MCLABELS
2020/10/13 全球购物
英语故事演讲稿
2014/04/29 职场文书
离职保密承诺书
2014/05/28 职场文书
学习三严三实心得体会
2014/10/13 职场文书
水知道答案观后感
2015/06/08 职场文书
贫困生证明范文
2015/06/16 职场文书
关于EntityWrapper的in用法
2022/03/22 Java/Android
本地搭建minio文件服务器(使用bat脚本启动)的方法
2022/07/15 Servers
win10拖拽文件时崩溃怎么解决?win10文件不能拖拽问题解决方法
2022/08/14 数码科技