PHP面向对象简易验证码类
PHP简易验证码类
<?php class authCode
{
private static $instance = null; #实例对象
private $width = 120; #图片宽度
private $height = 40; #图片高度
private $font = 'font/elephant.ttf'; #字体文件路径
private $fontSize = 14; #字体大小
private $strLen = 6; #字符个数
private $auth_code_str = null; #验证码结果
private $imgResult = null; #图片资源 #入口文件 静态方法调用 实例化对象 可用 对象方法调用
public static function img()
{
if (!(self::$instance instanceof self)) {
self::$instance = new self();
}
return self::$instance;
} #随机颜色
private function randomColor($img = null, $min = 0, $max = 255)
{
$rgb = [];
for ($i = 1; $i <= 3; $i++) {
$rgb[] = str_pad(rand($min, $max), 3, 0, STR_PAD_LEFT);
}
return imagecolorallocate($img, $rgb[0], $rgb[1], $rgb[2]);
} #随机字符串
private function randomStr($num = 4)
{
if ($num > 0) {
$string = array_merge(range('a', 'z'), range(0, 9), range('A', 'Z'), range(0, 9));
for ($i = 1; $i <= $num; $i++) {
shuffle($string);
$this->auth_code_str .= array_pop($string);
}
}
return $this;
} #创建验证码
public function createAuthCode(&$codeStr = false)
{
if (!$this->auth_code_str) {
$this->randomStr($this->strLen);
} if ($codeStr !== false && empty($codeStr)) {
$codeStr = $this->auth_code_str;
} else if (!empty($codeStr) && $codeStr !== false) {
$this->auth_code_str = $codeStr;
} $this->imgResult = imagecreatetruecolor($this->width, $this->height); $background = $this->randomColor($this->imgResult, 200); imagefilledrectangle($this->imgResult, 0, 0, $this->width, $this->height, $background); $y = ($this->height - $this->fontSize); $string = str_split($this->auth_code_str, 1); for ($i = 0; $i < count($string); $i++) {
$frontColor = $this->randomColor($this->imgResult, 0, 200);
imagefttext($this->imgResult, $this->fontSize, rand(0, 10), ($this->fontSize + 2) * $i + 10, $y, $frontColor, $this->font, $string[$i]);
}
return $this;
} #生成线
public function line($line = 3)
{
$line = $line ?: 3;
for ($i = 1; $i <= $line; $i++) {
$lineColor = $this->randomColor($this->imgResult, 0, 200);
imageline($this->imgResult, rand(0, $this->width / 5), rand(5, $this->height - 5), rand($this->width / 1.3, $this->width), rand(5, $this->height - 5), $lineColor);
}
return $this;
} #噪点
public function pixel($num = 50){
$num = $num ?: 3;
for ($i = 1; $i <= $num; $i++) {
$lineColor = $this->randomColor($this->imgResult, 0, 100);
imagesetpixel($this->imgResult, rand(0, $this->width), rand(0, $this->height), $lineColor);
}
return $this;
} #设置大小
public function size($width = null, $height = null)
{
$this->width = $width ?: 120;
$this->height = $height ?: 40;
return $this;
} #设置字体大小
public function fontSize($fontsize = 14)
{
$this->fontSize = $fontsize ?: 14;
return $this;
} #设置字体
public function font($file = null)
{
if (is_null($file) === true) {
$this->font = 'font/elephant.ttf';
} else {
$this->font = $file;
}
return $this;
} #设置长度
public function strlen($num = null)
{
$this->strLen = $num ?: 6;
return $this;
} public function display()
{
ob_end_flush();
header("content-type:image/jpeg");
imagejpeg($this->imgResult, null, 100);
imagedestroy($this->imgResult);
exit;
}
} #简单调用方法
authCode::img()->createAuthCode()->display(); #指定字符串调用
// $string = 'abc123';
// authCode::img()->createAuthCode($string)->display(); #设置图片大小、字数、字体大小
// authCode::img()->strlen(8)->size(300,100)->fontSize(30)->createAuthCode()->display(); #添加噪点
// authCode::img()->createAuthCode()->line()->pixel()->display(); ?>
以上便是关于验证码类的封装过程,可以直接使用。
链接:https://mp.weixin.qq.com/s/7RrGadoaN-If72N30LGEEw
PHP面向对象简易验证码类的更多相关文章
- PHP 简单面向对象 验证码类(静态实例对象调用)
没事写了一个简单的面向对象验证码类,可以直接使用(替换一下字体路径) <?php class authCode { private static $instance = null; #实例对象 ...
- php面向对象(OOP)---- 验证码类
PHP常用自封装类--验证码类 验证码是众多网站登陆.注册等相关功能不可以或缺的功能,实现展示验证码的方式有很多,这篇文章作者以工作中比较常用的方法进行了封装. 逻辑准备 要实现一个完整的验证码,需要 ...
- PHP验证码类
通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现的细节封装在该类中.只要在创建对象时,为构造方法提供三个参数 ...
- 一个经典的PHP验证码类分享
我们通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现 的细节封装在该类中.只要在创建对象时,为构造方法提供三 ...
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- THINKPHP源码学习--------验证码类
TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...
- ThinkPHP 3.2.3 加减乘法验证码类
ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Ver ...
- 一个漂亮的php验证码类(分享)
直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...
- 【个人使用.Net类库】(4)验证码类
验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...
随机推荐
- swap的几点理解
一.什么是swap space(交换分区)? 在Linux系统中,当物理内存满了才使用Swap空间.当系统需要更多的内存资源,并且物理内存已经满了,此时,内存中那些不活跃的pages被移动(move) ...
- 1.MVC的初步了解
1.MVC简单介绍 1)原理(如图)如果想更加深入了解原理,可以访问此地址(https://www.cnblogs.com/uicodeintoworld/p/8950877.html) 理解:客户端 ...
- string.format()详解
java中: 从 Java 5.0 开始,String 类新增了一个强大的字符串格式化方法 format().这个方法到现在用的人还是不多,实在是一种浪费.本文带你快速过一遍这个方法的功能,将来你要用 ...
- 【leetcode】424. Longest Repeating Character Replacement
题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...
- 转载:有好多是Elastic官方博客上的内容,具体看文档
Elastic官方博客地址:https://blog.csdn.net/UbuntuTouch 这个地址是在CSDN上面的,看不惯CSDN网站的广告,同时官方博客讲述的也都十分详细,自己保留一份作为参 ...
- 阿里云重磅发布RDS for SQL Server AlwaysOn集群版
2018年双十一刚过,阿里云数据库发布RDS for SQL Server AlwaysOn集群版,这是业界除微软云SQL Database外,首家云计算公司基于SQL Server最新AlwaysO ...
- bzoj 2015
http://www.lydsy.com/JudgeOnline/problem.php?id=2015 裸最短路(' ' ) 不过我最初以为是mst (' ' ) #include & ...
- nginx调优配置
nginx调优配置 user www www; #工作进程:数目.根据硬件调整,通常等于CPU数量或者2倍于CPU. worker_processes 8; worker_cpu_affinity 0 ...
- JAVA Web学习笔记
JAVA Web学习笔记 1.JSP (java服务器页面) 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . JSP全名为Java Server Pages,中文名叫java服务器 ...
- Spring MVC请求参数绑定 自定义类型转化 和获取原声带额servlet request response信息
首先还在我们的框架的基础上建立文件 在domian下建立Account实体类 import org.springframework.stereotype.Controller; import org. ...