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面向对象简易验证码类的更多相关文章

  1. PHP 简单面向对象 验证码类(静态实例对象调用)

    没事写了一个简单的面向对象验证码类,可以直接使用(替换一下字体路径) <?php class authCode { private static $instance = null; #实例对象 ...

  2. php面向对象(OOP)---- 验证码类

    PHP常用自封装类--验证码类 验证码是众多网站登陆.注册等相关功能不可以或缺的功能,实现展示验证码的方式有很多,这篇文章作者以工作中比较常用的方法进行了封装. 逻辑准备 要实现一个完整的验证码,需要 ...

  3. PHP验证码类

    通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现的细节封装在该类中.只要在创建对象时,为构造方法提供三个参数 ...

  4. 一个经典的PHP验证码类分享

    我们通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现 的细节封装在该类中.只要在创建对象时,为构造方法提供三 ...

  5. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  6. THINKPHP源码学习--------验证码类

    TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...

  7. ThinkPHP 3.2.3 加减乘法验证码类

    ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Ver ...

  8. 一个漂亮的php验证码类(分享)

    直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...

  9. 【个人使用.Net类库】(4)验证码类

    验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...

随机推荐

  1. Linux中的系统服务_01

    Linux中的系统服务_01 1.linux中所有的内容都是以文件的形式存在,所以其服务也是以文件的形式存在/etc/init.d目中, 2.存在改/etc/init.d/目录里的文件,都可以通过命令 ...

  2. 【leetcode】941. Valid Mountain Array

    题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...

  3. 流量隔离方案 Dpath 护航双十一新零售

    需求 在今年的双11准备期间,业务同学提出要针对新零售进行特殊的保障,希望新零售过来的流量,单独进入到一批机器,和其他普通流量隔离开来,这对新零售系统稳定性提出更高的要求. 需求总结下来就是: 针对特 ...

  4. bzoj 2015

    http://www.lydsy.com/JudgeOnline/problem.php?id=2015 裸最短路(' '     ) 不过我最初以为是mst (' '    ) #include & ...

  5. IDEA2019.1.3最新破解方式

    版本2019.1.3 1.下载破解JAR,放入IDEA的bin文件夹中     链接:https://pan.baidu.com/s/1N1BHeJ0-mmFIWbrh5h4k-g     提取码:g ...

  6. struts2 值栈分析

    目录 一.值栈分为两个逻辑部分 二.Struts2 利用 s:property 标签和 OGNL表达式来读取值栈中的属性值 1.值栈中的属性值: 2.读取对象栈中对象的属性: 3.默认情况下,Acti ...

  7. Node.js 开发技能图谱

    # Node.js 开发技能图谱 ## Node.js 语言环境搭建 - Node.js 安装(3m大法:nvm.npm.nrm)- Node.js 命令- Node.js开发工具(推荐vscode) ...

  8. Ubuntu安装可视化电脑配置视图工具neofetch

    安装步骤: sudo apt-get install software-properties-common python-software-propertiessudo add-apt-reposit ...

  9. php自带函数大全

    php自带函数大全 http://blog.csdn.net/hopewtc/article/details/6797326   Abs: 取得绝对值.Acos: 取得反余弦值.ada_afetch: ...

  10. Windows 8.1 PLSQL_32连接到RHEL6.1 Oracle10gr2_64

    目录 目录 系统环境 连接Oracle Server 系统环境 操作系统 Windows 8.1 RHEL6.1 软件 Oracle10gr2 PL/SQL instantclient-basic-w ...