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- ...
随机推荐
- python raise异常处理
python raise异常处理 一般最简单的异常处理是try except: try: f = open('test.txt') except Exception as e: print(e) f ...
- 15、SpringMVC之常用组件及执行流程
15.1.常用组件 15.1.1. DispatcherServlet DispatcherServlet 是前端控制器,由框架提供,不需要工程师开发: 作用:统一处理请求和响应,整个流程控制的中心, ...
- 【Ubuntu】下载安装 20.04.版本 桌面端
下载 Download 这次的是直接在界面上下载的,我都不知道为什么怎么点到之前的版本去了 12.04.5远古版本界面怪难看的... [下载地址:点我访问] https://cn.ubuntu.com ...
- NVIDIA的人形机器人的基础模型Project GR00T已在实体机器人上进行展示
原文地址: https://blogs.nvidia.com/blog/isaac-generative-ai-manufacturing-logistics/ 项目GR00T为人型机器人开发谢幕 在 ...
- 【安装】SQL SERVER 彻底卸载清除
-----2024年8月6日09:40:13 -----bayaim, 以下内容纯属百度网络搜到,如有侵权请联系及时删除 SQL SERVER 如果卸载不干净,就会导致下一次安装失败,下面是卸载的步 ...
- @ComponentScan
@ComponentScan 是一个注解,用于Spring框架,它允许开发者指定Spring应该扫描哪个包或包下的子包来寻找组件(如@Component.@Service.@Repository等注解 ...
- SpringBoot优雅开发REST API最佳实践
写在前面 博主最近在做一个数据服务的项目,而这个数据服务的核心就是对外暴露的API,值得高兴的这是一个从0开始的项目,所以终于不用受制于"某些历史"因素去续写各种风格的Contro ...
- canvas实现手动绘制矩形
开场白 虽然在实际的开发中我们很少去绘制流程图 就算需要,我们也会通过第3方插件去实现 下面我们来简单实现流程图中很小的一部分 手动绘制矩形 绘制一个矩形的思路 我们这里绘制矩形 会使用到canvas ...
- 面试必问之kafka
问题1:消息队列的作用 1. 解耦 快递小哥手上有很多快递需要送,他每次都需要先电话一一确认收货人是否有空.哪个时间段有空,然后再确定好送货的方案.这样完全依赖收货人了!如果快递一多,快递小哥估计的忙 ...
- AREA |.text|, CODE, READONLY, ALIGN=2详解
AREA |.text|, CODE, READONLY, ALIGN=2 ;AREA |.text| 选择段 |.text|. ;CODE表示代码段,READONLY表示只读(缺省) ...