php生成验证码类
php生成验证码类
直接看代码
<?php
session_start();
class Code{ //资源
private $img;
//画布宽度
private $width=100;
//画布高度
private $height=30;
//背景颜色
private $bgColor='#ffffff';
//验证码
private $code;
//验证码的随机种子
private $codeStr='23456789abcdefghjkmnpqrstuvwsyz';
//验证码长度
private $codeLen=4;
//验证码字体
private $font;
//验证码字体大小
private $fontSize=16;
//验证码字体颜色
private $fontColor=''; public function __construct() {
} //创建验证码
public function make()
{
if(empty($this->font))
{
$this->font = base_path().'\public\out\consola.ttf';
}
$this->create();//生成验证码
header("Content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
exit;
} //设置字体文件
public function font($font)
{
$this->font= $font;
return $this;
} //设置文字大小
public function fontSize($fontSize)
{
$this->fontSize=$fontSize;
return $this;
} //设置字体颜色
public function fontColor($fontColor)
{
$this->fontColor = $fontColor;
return $this;
} //验证码数量
public function num($num)
{
$this->codeLen=$num;
return $this;
} //设置宽度
public function width($width)
{
$this->width = $width;
return $this;
} //设置高度
public function height($height)
{
$this->height = $height;
return $this;
} //设置背景颜色
public function background($color)
{
$this->bgColor = $color;
return $this;
} //返回验证码
public function get() {
return $_SESSION['code'];
} //生成验证码
private function createCode() {
$code = '';
for ($i = 0; $i < $this->codeLen; $i++) {
$code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)];
}
$this->code = strtoupper($code);
$_SESSION['code'] = $this->code;
} //建画布
private function create() {
if (!$this->checkGD())
return false;
$w = $this->width;
$h = $this->height;
$bgColor = $this->bgColor;
$img = imagecreatetruecolor($w, $h);
$bgColor = imagecolorallocate($img, hexdec(substr($bgColor, 1, 2)), hexdec(substr($bgColor, 3, 2)), hexdec(substr($bgColor, 5, 2)));
imagefill($img, 0, 0, $bgColor);
$this->img = $img;
$this->createLine();
$this->createFont();
$this->createPix();
$this->createRec();
} //画线
private function createLine(){
$w = $this->width;
$h = $this->height;
$line_color = "#dcdcdc";
$color = imagecolorallocate($this->img, hexdec(substr($line_color, 1, 2)), hexdec(substr($line_color, 3, 2)), hexdec(substr($line_color, 5, 2)));
$l = $h/5;
for($i=1;$i<$l;$i++){
$step =$i*5;
imageline($this->img, 0, $step, $w,$step, $color);
}
$l= $w/10;
for($i=1;$i<$l;$i++){
$step =$i*10;
imageline($this->img, $step, 0, $step,$h, $color);
}
} //画矩形边框
private function createRec() {
//imagerectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->fontColor);
} //写入验证码文字
private function createFont() {
$this->createCode();
$color = $this->fontColor;
if (!empty($color)) {
$fontColor = imagecolorallocate($this->img, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
}
$x = ($this->width - 10) / $this->codeLen;
for ($i = 0; $i < $this->codeLen; $i++) {
if (empty($color)) {
$fontColor = imagecolorallocate($this->img, mt_rand(50, 155), mt_rand(50, 155), mt_rand(50, 155));
}
imagettftext($this->img, $this->fontSize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontColor, $this->font, $this->code [$i]);
}
$this->fontColor = $fontColor;
} //画线
private function createPix() {
$pix_color = $this->fontColor;
for ($i = 0; $i < 50; $i++) {
imagesetpixel($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
} for ($i = 0; $i < 2; $i++) {
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
}
//画圆弧
for ($i = 0; $i < 1; $i++) {
// 设置画线宽度
imagearc($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height)
, mt_rand(0, 160), mt_rand(0, 200), $pix_color);
}
imagesetthickness($this->img, 1);
} //验证GD库
private function checkGD() {
return extension_loaded('gd') && function_exists("imagepng");
} }
php生成验证码类的更多相关文章
- PHP常用类------生成验证码类Code
直接附上代码吧!很简单的代码,写一遍基本就会了,主要明白用GD库画图的几个步骤: 1.先创建画布,不然画在哪儿?(imagecreatetruecolor)2.根据要画的数据,选定颜色 (imagec ...
- C#生成验证码类
using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;us ...
- Asp.net mvc生成验证码
1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- Laravel 下生成验证码的类
<?php namespace App\Tool\Validate; //验证码类 class ValidateCode { private $charset = 'abcdefghkmnprs ...
- JavaWeb学习总结(七):通过Servlet生成验证码及其应用 (BufferedImage类)
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- THINKPHP源码学习--------验证码类
TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...
- 动态生成验证码———MVC版
上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...
- java web学习总结(九) -------------------通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- PHP生成验证码及单实例应用
/* note: * this 指向当前对象本身 * self 指向当前类 * parent 指向父类 */ /* 验证码工具类 * @author pandancode * @date 20150- ...
随机推荐
- Vue cil路由如何回到初始状态
前景:我们在网页里进入路由的地址后,会发现地址栏中会加上我们的路由地址,这样我就知道当前在哪个位置.但是这样子我们如何手动刷新浏览器,想要浏览器回到根路径的话,是无法直接回去的,因为地址没有更改.再怎 ...
- 加油,为Vue3提供一个可媲美Angular的ioc容器
为什么要为Vue3提供ioc容器 Vue3因其出色的响应式系统,以及便利的功能特性,完全胜任大型业务系统的开发.但是,我们不仅要能做到,而且要做得更好.大型业务系统的关键就是解耦合,从而减缓shi山代 ...
- 【Mybatis】记录下一些问题
报错信息: 找不到映射的结果Map 其实这里的包的名字和资源的名字都是正确的 但是啊,但是啊,在Mapper.xml上面的命名空间的声明上换行了,这就能导致Mybatis找不到这个资源: 我和同事看了 ...
- L-BFGS-B(Limited-memory Broyden–Fletcher–Goldfarb–Shanno )算法理解 —— 内存受限的拟牛顿法 —— 数值优化算法
本文主要讲下个人对数值优化算法中几种常见算法的理解. 什么是优化算法? 给出函数f(X),现在要求 min f(X) 时的X值,这就是最优化问题. 1. 共轭梯度法 方程:A*x=b,A矩阵为对称正定 ...
- Google的TPU的向量化内存的读取规格——单次读取/写入的数据量
异构加速设备: GPU.TPU.NPU 这几种设备,除了GPU公开了部分硬件设计原理和软件编程范式以外,所有的TPU和NPU的资料都是不公开的,都是被其所属公司保留的,然后这些公司会自己在这些硬件之上 ...
- 带有最小间隔时间的队列读取实现 —— 最小等待时间的队列 —— Python编程(续)
接前文: 带有最小间隔时间的队列读取实现 -- 最小等待时间的队列 -- Python编程 由于上次的设计多少有些简单,这里对此丰富一下. ============================== ...
- java模拟并发请求工具类(测试专用)
1.背景 实际生产中,我们开发好接口后可能会简单的压力测试一下,也就是说模拟并发测试,测试工具类如下: 2.工具类 package tentative.normal.other; import cn. ...
- AC自动机 基础篇
AC 自动机1 前置知识:\(KMP\),字典树. \(AC\) 自动机,不是用来自动 \(AC\) 题目的,而是用来处理字符串问题的(虽然确实可以帮助你 \(AC\)). 这里总结了 \(AC\) ...
- springboot2集成oauth2坑一(Possible CSRF detected - state parameter was required but no state could )
码云地址:https://gitee.com/lpxs/lp-springcloud.git 有问题可以多沟通:136358344@qq.com. 刚开始用springboot1.5集成oauth2没 ...
- 【问答23】Linux移植:如何制作rootfs?
粉丝问题 如何制作rootfs? 安排! 想直奔主题的,直接跳到第四章. 一.分析 1. 文件系统简介 理论上说一个嵌入式设备如果内核能够运行起来,且不需要运行用户进程的话,是不需要文件系统的,文件系 ...