<?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 下生成验证码的类的更多相关文章

  1. Eclipse下生成/编辑Java类图或时序图(UML)[转载]

    一 引用文章 1.[eclipse下生成Java类图和时序图,生成UML图(更完整版)](https://blog.csdn.net/guomainet309/article/details/5302 ...

  2. 6月19 使用tp框架生成验证码及文件上传

    ThinkPHP中自带能生成验证码的类:ThinkPHP/Library/Think/Verify.class.php 默认情况下,验证码的字体是随机使用 ThinkPHP/Library/Think ...

  3. 动态生成验证码———MVC版

    上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...

  4. C#生成验证码

    生成验证码的类: using System; using System.Collections.Generic; using System.Drawing; using System.Text; na ...

  5. ASP.NET MVC5 生成验证码

    1 ValidateCode.cs using System; using System.Drawing; using System.Drawing.Drawing2D; using System.D ...

  6. java生成验证码并可刷新

    手心创建一个简单的页面来显示所创建的验证码 <body> <form action="loginName.mvc" method="post" ...

  7. (一)【转】asp.net mvc生成验证码

    网站添加验证码,主要为防止机器人程序批量注册,或对特定的注册用户用特定程序暴力破解方式,以进行不断的登录.灌水等危害网站的操作.验证码被广泛应用在注册.登录.留言等提交信息到服务器端处理的页面中.   ...

  8. laravel 生成验证码的方法

    在Laravel中有很多图片验证码的库可以使用,本篇介绍其中之一:gregwar/captcha,这个库比较简单,在Laravel中比较常用.下面我们就来介绍下使用细节: 首先, composer.j ...

  9. JavaWeb学习总结(七):通过Servlet生成验证码及其应用 (BufferedImage类)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

随机推荐

  1. Oracle minus用法详解及应用实例

    本文转载:https://blog.csdn.net/jhon_03/article/details/78321937 Oracle minus用法 “minus”直接翻译为中文是“减”的意思,在Or ...

  2. array_merge

    1.array_merge 中有两个参数:将两个关联数组合并为一个数组 <?php $a1=array("a"=>"red","b&quo ...

  3. Xml的用途

    1.可以作为数据库存储数据--通过XML文件存储数据,可以通过javaScript读取外部的XML文件,然后更新HTML的数据内容 2.XML数据以纯文本格式进行存储,提供了独立于软件和硬件的数据存储 ...

  4. P1280 尼克的任务 dp

    思路: 倒着DP  f[i]表示i时刻的空闲时间最大值 在当前时间没有任务开始 f[i]=f[i+1]+1;    上一分钟最大空闲时间+1 在当前时间有任务开始  f[i]=max(f[i],f[i ...

  5. scrapy爬取知乎问答

    登陆 参考 https://github.com/zkqiang/Zhihu-Login # -*- coding: utf-8 -*- import scrapy import time impor ...

  6. [CEOI2007] 树的匹配Treasury

    类型:树形 DP 传送门:>Here< 题意:给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配是多少,并且计算出有多少种最大匹配. 解题思路 首先树形Dp是很明显的,$f[i][ ...

  7. python中切片的理解

    Python中什么可以切片 l  Python中符合序列的有序序列都支持切片(slice) l  如:列表,字符,元祖 Python中切片的格式 l  格式:[start : end : step] ...

  8. Ionic3实现选项卡切换可以重新加载echarts

    要求 选项卡每切换一次,就加载对应的一个echarts 图,要有一开始的动画效果效果如下: 注意点 1.echarts要想每次都能重新加载,需要移除"_echarts_instance_&q ...

  9. ueditor 编辑器常用方法

    官方文档http://fex.baidu.com/ueditor/ 简单实现工具栏调用: //实现方法 ue.getDialog("工具名称"); //工具名称可以参考如下: to ...

  10. AMH 软件目录介绍

    AMH系统shell脚本目录:/root/amh系统所有shell脚本文件目录,不可删除. 网站运行工作根目录:/home/wwwroot面板程序与新建虚拟主机网站都存放于此目录. 其中:/home/ ...