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);
}
}
Laravel 下生成验证码的类的更多相关文章
- Eclipse下生成/编辑Java类图或时序图(UML)[转载]
一 引用文章 1.[eclipse下生成Java类图和时序图,生成UML图(更完整版)](https://blog.csdn.net/guomainet309/article/details/5302 ...
- 6月19 使用tp框架生成验证码及文件上传
ThinkPHP中自带能生成验证码的类:ThinkPHP/Library/Think/Verify.class.php 默认情况下,验证码的字体是随机使用 ThinkPHP/Library/Think ...
- 动态生成验证码———MVC版
上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...
- C#生成验证码
生成验证码的类: using System; using System.Collections.Generic; using System.Drawing; using System.Text; na ...
- ASP.NET MVC5 生成验证码
1 ValidateCode.cs using System; using System.Drawing; using System.Drawing.Drawing2D; using System.D ...
- java生成验证码并可刷新
手心创建一个简单的页面来显示所创建的验证码 <body> <form action="loginName.mvc" method="post" ...
- (一)【转】asp.net mvc生成验证码
网站添加验证码,主要为防止机器人程序批量注册,或对特定的注册用户用特定程序暴力破解方式,以进行不断的登录.灌水等危害网站的操作.验证码被广泛应用在注册.登录.留言等提交信息到服务器端处理的页面中. ...
- laravel 生成验证码的方法
在Laravel中有很多图片验证码的库可以使用,本篇介绍其中之一:gregwar/captcha,这个库比较简单,在Laravel中比较常用.下面我们就来介绍下使用细节: 首先, composer.j ...
- JavaWeb学习总结(七):通过Servlet生成验证码及其应用 (BufferedImage类)
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
随机推荐
- 所活天数!java Date应用
package cn.jiu.com; import java.text.ParseException; import java.text.SimpleDateFormat; import java. ...
- JarvisOJ Basic veryeasyRSA
已知RSA公钥生成参数: p = 3487583947589437589237958723892346254777 q = 8767867843568934765983476584376578389 ...
- 创建第一个Djiago
Djiago 目录介绍 mysite/ ├── manage.py # 管理文件 └── mysite # 项目目录 ├── __init__.py ├── settings.py # 配置 ├── ...
- CNN卷积神经网络人脸识别
图片总共40个人,每人10张图片,每张图片高57,宽47.共400张图片. 读取图片的py文件 import numpyimport pandasfrom PIL import Imagefrom k ...
- 学习Android过程中遇到的问题及解决方法——AS为xutils添加依赖
在使用xutils时遇到不能添加以来的问题,花了很长时间终于解决,网上添加依赖的方法很多,在此针对个人出现的问题作下笔记. 我本想使用jar包,因为在使用smartImageView时是用的jar包来 ...
- 安卓Android基础—第二天
测试的相关概念 好的软件不是开发出来的,是不断测试出回来的 根据是否知道源代码 黑盒测试 白盒测试 根据测试的粒度 方法测试 单元测试 集成测试 系统测试 根据测试的暴力程度 压力测试(谷歌工程师提供 ...
- [luogu2286][HNOI2004]宠物收养场【平衡树】
[传送门] 前言 这一篇题解并不是为了讲什么算法,只是总结一下平衡树在OI考试中的注意事项. 题意简化(给不想看题目的小伙伴们一点福利) 给你两堆数,每一次给你一个数,每一次在另外一堆数中找到这个数的 ...
- PHP-FPM监控shell
!/bin/bash #监控的网页地址url="http://dev2.jwsmed.com" #fastcgi启动/重启/停止脚本路径PROG=/data/fistsoft/ph ...
- Intervals POJ - 3680 (MCMF)
给你一些区间,每个区间都有些价值.取一个区间就能获得对应的价值,并且一个点不能覆盖超过k次,问你最大的价值是多少. 我们可以把这些区间放到一维的轴上去,然后我们可以把它看成一个需要从左到右的过程,然后 ...
- NOIP2012疫情控制(二分答案+树上贪心)
H 国有n个城市,这 n个城市用n-1条双向道路相互连通构成一棵树,1号城市是首都,也是树中的根节点. H国的首都爆发了一种危害性极高的传染病.当局为了控制疫情,不让疫情扩散到边境城市(叶子节点所表示 ...