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- ...
随机推荐
- Windows下UI自动化工具Inspect
windows系统下的UI自动化工具Inspect是包含在WindowsSDK包里的,所以需要先下载SDK包,然后在SDK包路径下找到Inspect工具并打开使用就可以了. 官网下载链接:https: ...
- argparse学习笔记
argparse是 Python 的一个内置模块,用于编写用户友好的命令行接口.使用 argparse,你可以很容易地为 Python 脚本添加参数解析功能,使得脚本可以接受命令行选项和参数.学起来也 ...
- 7、Git之Github操作
7.1.注册Github账号 7.1.1.访问官网 Github 官网:https://github.com/ 先访问GitHub的官网首页,点击 sign in (登录),跳转到登录页. 7.1.2 ...
- 【Java】使用Druid连接池的监控面板排查慢SQL
默认在后台服务的地址: http://localhost:8078/druid/login.html 账号信息放在配置文件中获取: server: port: 8078 spring: datasou ...
- 【JavaWeb】如何越过SpringMVC直接返回内容
来自前同事问的一个问题,因为项目里面的SpringMVC会封装好一个固定的JSON响应规范: 可以看见,data属性下面,又会有一层data, 数据的消费方提出要求,只需要里面data的数据,外面的J ...
- ( Ubuntu环境下 ) Vim插件管理器 Vundle 的安装
为vim安装插件的时候一般会选择先安装一个插件管理器, 一般有 Pathogen和Vundle 比较常用,同时也有比较轻量级的 Vim插件管理神器vim-plug . 1. 下载 Vim插 ...
- vue 文件流下载
/** * 文件转为文件流 * @param {file} file //文件 */ export function getFileBlob(file) { var dataUrl va ...
- Vue-购物车实战
computed 计算属性 正则 css部分 [v-cloak] { display : none ; } table{ border : lpx solid #e9e9e9 ; border- co ...
- Linux入门的基础知识点,有这篇就够了(持续更新)
很多粉丝给一口君留言,想要学习Linux资料,其实关注一口君的公众号,后台回复 1024 ,就有很多非常不错的电子书,但是有一个问题,很多粉丝是初学者,而这一大堆电子书,估计随便一本,还没看完就基本上 ...
- Buckingham-Reiner 方程和 Darby-Melson 经验方程
由 Roni, et al. (2018), Woolley, et al. (2014), Yang, et al. (2017) 整理人便便的物理性质数据如下: 性质 值 密度 \((\text{ ...