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)验证码类
验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...
随机推荐
- JDBC、ibatis(mybatis)、Hibernate有什么不同?
①JDBC编程流程固定,同时将sql语句和java代码混在了一起,经常需要拼凑sql语句,细节很繁琐: ②ibatis(mybatis)它不完全是一个ORM框架,因为MyBatis需要程序员自己编写S ...
- 【串线篇】spring泛型依赖注入原理
spring泛型依赖注入原理 不管三七二十一 servlet :加注解@servlet service:加注解@service dao:加注解@Repository 这相当于在容器中注册这些个类
- 浅谈Java反射与框架
Java反射 1.示例 1.用户类 package com.lf.entity; import com.lf.annotation.SetProperty; import com.lf.annotat ...
- 03.线程的通知notify与等待wait
wait().notify.notifyAll()方法 wait().notify().notifyAll()是三个定义在Object类里的方法,可以用来控制线程的状态. 这三个方法最终调用的都是jv ...
- OpenCV常用基本处理函数(7)图像金字塔和直方图
高斯金字塔 高斯金字塔的顶部是通过将底部图像中的连续的行和列去除得到的.顶部图像中的每个像素值等于下一层图像中 5 个像素的高斯加权平均值. 这样操作一次一个 MxN 的图像就变成了一个 M/2xN/ ...
- SQL Join连接
SQL 连接(Joins) SQL join 用于把来自两个或多个表的行结合起来. SQL JOIN SQL JOIN 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段. 最常见的 J ...
- jdbc——java连接sql server 过程
首先要去下一个关于sql的驱动jar包,叫做sqljdbc4.jar 然后更新项目的build path,加入这个jar包 前几步有问题的看该博客 https://blog.csdn.net/qq24 ...
- 让vue用于小程序setData方法
setData:function(obj){ let that = this; let keys = []; let val,data; Object.keys(obj).forEach(functi ...
- 如何将当前平台升级到SonarQube7.9?[最新]
整体思路 准备测试数据(实际环境可跳过此步骤) 数据库迁移(从版本7.9开始,SonarQube将不再支持MySQL,Mysql-->PG) Sonar版本升级(6.7.7 -> 7.9. ...
- php中的list()
list()在php中上一个语言结构,并不是一个函数.类似array(),不过array()这个东西我们现在一般很少使用了,因为从php5.4版本开始,我们会直接使用[]来定义数组. 那么,list( ...