PHP 之验证码类封装
2024-10-06 15:09:45
一、效果图
二、类代码
<?php
/**
* Created by PhpStorm.
* User: Yang
* Date: 2019/8/13
* Time: 10:51
*/ class Captcha
{
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 = dirname(__FILE__) . '/font/Elephant.ttf';//注意字体路径要写对,否则显示不了图片
} //生成随机码
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 createImage()
{
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
} //获取验证码
public function getCode()
{
return strtolower($this->code);
}
}
三、使用
<?php
/**
* Created by PhpStorm.
* User: Yang
* Date: 2019/8/13
* Time: 11:02
*/ include "./Captcha.php"; $c = new Captcha();
$c->createImage();
四、源代码
链接:https://pan.baidu.com/s/1i2MNN-y0xl4Qsfhn0EhGJQ
提取码:qtfx
最新文章
- Zabbix监控mysql performance
- 基于AgileEAS.NET SOA 中间件领域模型数据器快速打造自己的代码生成器
- jQuery 点击显示再次点击隐藏
- Win7(32bit)下Qt5.5.0和OpenCV2.4.9环境的搭建
- 自然语言16.1_Python自然语言处理学习笔记之信息提取步骤&;分块(chunking)
- 攻城狮在路上(壹) Hibernate(十四)--- Hibernate的检索方式(下)
- Windows Server 2012下安装Hyper-V虚拟机
- HDU-4628 Pieces 搜索 | DP
- qemu 调试(二)
- Android中的一些基础知识(二)
- paip.jdk1.4 1.5(5.0) 1.6(6.0) 7.0 8.0特点比较与不同
- c#的as关键字
- c++字符串的输入的思考
- Milk Patterns poj3261(后缀数组)
- 基于zookeeper的Swarm集群搭建
- Linux 系统下在线安装 Tomcat
- zookeeper的三种安装模式
- vue cli3.0 结合echarts3.0和地图的使用方法
- html 刷新重载方法汇总
- 2080 特殊的质数肋骨 USACO (深度优先搜索)