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

<?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. Android TextView文本处理库推荐

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/115 Android TextView文本处理库推荐 现在 ...

  2. CDH预警配置QQ邮箱

    一. 在QQ邮箱中开启POP   二 .关闭主机的sendmail,开启postfix (本机若没有两个服务,就需要先安装)本地安装sendmail和postfix [root@Slave1 ~] ...

  3. 设备树处理之——device_node转换成platform_device【转】

    转自:https://www.cnblogs.com/downey-blog/p/10486568.html 以下讨论基于linux4.14,arm平台 platform device 设备树的产生就 ...

  4. Day_04 面向对象

    概述 对于面向对象编程的支持,Go语言设计得非常简洁而优雅.因为,Go语言并没有沿袭传统 面向对象编程中的诸多概念,比如继承(不支持继承,尽管匿名字段的内存布局和行为类似继承,但它并不是继承). 虚函 ...

  5. 自定义MVC二

    1. 什么是MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写, 它是一种软件设计典范,用一种业务逻辑.数据. ...

  6. 27.Java基础_多态

    父类对象引用子类对象: Animal a=new Cat(); 多态中的转型 public class Animal { public void eat(){ System.out.println(& ...

  7. 解决N个人过桥时间最短问题(Java版本)

    [问题描述] n个人要晚上过桥,在任何时候最多两个人一组过桥,每组要有一只手电筒.在这n个人中只有一个手电筒能用,因此要安排以某种往返的方式来返还手电筒,使更多的人可以过桥.   注意:每个人的过桥速 ...

  8. pdfium ppm demo

    #include "fpdfview.h" #include <iostream> #include <string> #include <strin ...

  9. C++ 标准库 std::npos 表示 size_t 的最大值

    参见:https://en.cppreference.com/w/cpp/algorithm/find std::npos 表示 size_t 的最大值,常用于对查找结果成功与否的判断. #inclu ...

  10. <Array> 277 243 244 245

    277. Find the Celebrity knows(i, j): By comparing a pair(i, j), we are able to discard one of them 1 ...