没事写了一个简单的面向对象验证码类,可以直接使用(替换一下字体路径)

<?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();
*/

  

PHP 简单面向对象 验证码类(静态实例对象调用)的更多相关文章

  1. python进阶01 面向对象、类、实例、属性封装、实例方法

    python进阶01 面向对象.类.实例.属性封装.实例方法 一.面向对象 1.什么是对象 #一切皆对象,可以简单地将“对象”理解为“某个东西” #“对象”之所以称之为对象,是因为它具有属于它自己的“ ...

  2. Python面向对象:类、实例与访问限制

    首先记录下面向对象的名词: 对象:python万物皆对象,程序设计的东西在对象上体现. 类:具有相同属性和行为的对象的集合. 消息:各个对象之间通过消息相互联系. 方法:对象功能实现的过程. 封装:把 ...

  3. python 四种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,直接修改类属性的值

    三种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,类名就是类对象,city就是类变量, #coding=utf-8 class empl ...

  4. Python面向对象编程 -- 类和实例、访问限制

    面向对象编程 Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程 ...

  5. Python 面向对象编程——类定义与对象

    <类定义与对象声明> 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对 ...

  6. 面向对象(类,实例变量,方法定义,方法重载,构造方法,this,string类)

    面向对象 类是由属性和方法组成 类是所有创建对象的模板 实例变量有默认值 实例变量至少在本类范围中有效 实例变量与局部变量冲突时,局部变量优先 类中方法定义类似于函数定义 修饰符 返回值类型 方法名( ...

  7. Python 面向对象基础(类、实例、方法、属性封装)

    python是面向对象语言,一切皆对象. 面向过程: 变量和函数. “散落” 在文件的各个位置,甚至是不同文件中.看不出变量与函数的相关性,非常不利于维护,设计模式不清晰. 经常导致程序员,忘记某个变 ...

  8. python 面向对象二 类和实例

    一.类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法, ...

  9. Python—类和实例对象

    面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...

随机推荐

  1. 微信小程序初体验遇到的坑

    今天,2017年1月9日凌晨,微信小程序如约上线.2007年1月9日,整整10年前的今天,苹果的iPhone手机正式问世! 经不起新技术的诱惑了,想试着开发一下看看.刚开始遇到很多坑,在这里记录一下, ...

  2. iOS滑动手势UIPanGestureRecognizer 注意事项

    今天在做侧滑页面时,发现页面随着滑动手势而滑动,到临界点时,如果再滑动会出现抖动现象.找到解决办法是进入方法后先判断一次,再判断是在滑动范围内让页面滑动.遂将滑动手势(UIPanGestureReco ...

  3. 一文解读ITIL (转)

    首先声明自己不是ITIL方面的专家,特别是具体的规范细节,后面论述如有不当,请指正.但我为什么会提起它?主要是因为它和运维(IT服务管理)相关性太大了.早起的运维完全就是以ITIL来蓝本构建的,在当时 ...

  4. [20190530]ORACLE 18c - ALTER SEQUENCE RESTART.txt

    [20190530]ORACLE 18c - ALTER SEQUENCE RESTART.txt --//以前遇到要重置或者调整seq比较麻烦,我有时候采用比较粗暴的方式就是删除重建.--//18c ...

  5. 以聚合数据免费接口为例,通过 Class 类继承方法,让小程序实现项目化接口调用

    微信小程序数据来源,是通过接口实现的.但接口如何调,数据如何取?每个人都有不同的方法,下面以聚合数据免费接口为例. 配置接口 config.js 聚合数据请求接口需要以key作为参数. const c ...

  6. Linux中的文件和目录结构详解

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...

  7. index-css-添加类-移除类-toggleClass-attr

    1=>index()会返回当前元素在所有的兄弟元素里面的索引值用法:$("a").click(function(){ console.log($(this).index()) ...

  8. Jmeter Question 之 ‘批量执行SQL语句’

    第一步: MySql数据库:jdbc:mysql://ip:3306/数据库名?useUnicode=true&characterEncoding=utf8&allowMultiQue ...

  9. 《从HBase offheap到Netty的内存管理》

      JVM中的堆外内存(off-heap memory)与堆内内存(on-heap memory) 1. 堆内内存(on-heap memory) 1.1 什么是堆内内存 Java 虚拟机在执行Jav ...

  10. Paper | Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising

    目录 故事背景 网络结构 BN和残差学习 拓展到其他任务 发表在2017 TIP. 摘要 Discriminative model learning for image denoising has b ...