PHP学习之验证码类
<?php
$code = new Code();
$code->outImage();
class Code
{
//验证码个数
protected $number;
//验证码类型
protected $codeType;
//图像宽度
protected $width;
//图像高度
protected $height;
//图像资源
protected $image;
//验证码字符串
protected $code; /**
* Undocumented function
*
* @param integer $number
* @param integer $codeType
* @param integer $width
* @param integer $height
*/
public function __construct($number = 4, $codeType = 2, $width = 100, $height = 50)
{
//初始化自己的成员属性
$this->number = $number;
$this->codeType = $codeType;
$this->width = $width;
$this->height = $height; //生成验证码函数
$this->code = $this->createCode();
} /**
* 析构函数
* 释放图像资源
*/
public function __destruct()
{
//释放图像资源
imagedestroy($this->image);
} /**
* //魔术方法 通过对象获取保护的code
* $code = new Code();
* echo $code->code;
*
* @param [type] $name
* @return void
*/
public function __get($name)
{
if ($name == 'code') {
return $this->code;
}
return false;
} /**
* 获取验证码
*
* @return void
*/
protected function createCode()
{
//通过你的验证码类型给你生成不同的验证码
switch ($this->codeType) {
case 0: //纯数字
$code = $this->getNumberCode();
break;
case 1: //纯字母
$code = $this->getCharCode();
break;
case 2: //字母和数字混合
$code = $this->getNumCharCode();
break;
default:
die('不支持这种验证码类型');
}
return $code;
} /**
* 生成纯数字验证码
*
* @return void
*/
protected function getNumberCode()
{
// $startNum = pow(10, $this->number - 1);
// $endNum = pow(10, $this->number) - 1;
// $str = rand($startNum, $endNum);
// return $str;
$str = join('', range(0, 9));
return substr(str_shuffle($str), 0, $this->number);
} /**
* 生成纯字母验证码
*
* @return void
*/
protected function getCharCode()
{
$str = join('', range('a', 'z'));
$str = $str . strtoupper($str);
return substr(str_shuffle($str), 0, $this->number);
} /**
* 生成字母和数字验证码
*
* @return void
*/
protected function getNumCharCode()
{
$numStr = join('', range(0, 9));
$str = join('', range('a', 'z'));
$str = $numStr . $str . strtoupper($str);
return substr(str_shuffle($str), 0, $this->number);
} /**
* 创建画布
*
* @return void
*/
protected function createImage()
{
$this->image = imagecreatetruecolor($this->width, $this->height);
} /**
* 填充背景颜色
*
* @return void
*/
protected function fillBack()
{
imagefill($this->image, 0, 0, $this->lightColor());
} /**
* 随机生成浅颜色
*
* @return void
*/
protected function lightColor()
{
return imagecolorallocate($this->image, mt_rand(130, 255), mt_rand(130, 255), mt_rand(130, 255));
} /**
* 随机生成深颜色
*
* @return void
*/
protected function darkColor()
{
return imagecolorallocate($this->image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
} /**
* 将验证码字符串画到画布中
*
* @return void
*/
protected function drawChar()
{
$width = ceil($this->width / $this->number);
for ($i = 0; $i < $this->number; $i++) {
$x = mt_rand($i * $width+5, ($i + 1) * $width - 10);
$y = mt_rand(0, $this->height - 15);
imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
}
} /**
* 添加干扰项
*
* @return void
*/
protected function drawDisturb()
{
for ($i = 0; $i < 150; $i++) {
$x = mt_rand(0, $this->width);
$y = mt_rand(0, $this->height);
imagesetpixel($this->image, $x, $y, $this->lightColor());
}
} /**
* 输出并且显示
*
* @return void
*/
protected function show()
{
header('Content-Type:image/png');
imagepng($this->image);
} public function outImage()
{
//创建画布
$this->createImage();
//填充背景色
$this->fillBack();
//将验证码字符串画到画布中
$this->drawChar();
//添加干扰项
$this->drawDisturb();
//输出并且显示
$this->show();
}
}
运行效果:
PHP学习之验证码类的更多相关文章
- THINKPHP源码学习--------验证码类
TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- 【个人使用.Net类库】(4)验证码类
验证码是现在管理系统常用的一种保护用户帐户信息的一种功能. 验证码可以有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试,虽然这可能是我们登录麻烦一点,但是对用户的密码安全来 ...
- 简单实用的PHP验证码类
一个简单实用的php验证码类,分享出来 ,供大家参考. 代码如下: <?php /** @ php 验证码类 @ http://www.jbxue.com */ Class code { var ...
- ThinkPHP 3.2.3 加减乘法验证码类
ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Ver ...
- Yaf零基础学习总结5-Yaf类的自动加载
Yaf零基础学习总结5-Yaf类的自动加载 框架的一个重要功能就是类的自动加载了,在第一个demo的时候我们就约定自己的项目的目录结构,框架就基于这个目录结构来自动加载需要的类文件. Yaf在自启动的 ...
- 一个漂亮的php验证码类(分享)
直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...
- PHP编写的图片验证码类文件分享方法
适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...
- PHP之验证码类
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/6/20 * Time: 14:29 */ Class c ...
随机推荐
- react route使用HashRouter和BrowserRouter的区别-Content Security Policy img-src 404(Not found)
踩坑经历 昨天看了篇关于react-route的文章,说BrowserRouter比HashRouter好一些,react也是推荐使用BrowserRouter,毕竟自己在前端方面来说,就是个小白,别 ...
- ARM与x86 CPU架构对比
CISC(复杂指令集计算机)和RISC(精简指令集计算机)是当前CPU的两种架构.它们的区别在于不同的CPU设计理念和方法.早期的CPU全部是CISC架构,它的设计目的是CISC要用最少的机器语言指令 ...
- OSI网络通信工作流程的标准化 ----- 理论
OSI 七层模型 1 应用层 [提供用户服务,具体功能由特定的程序而定] 2 表示层 [数据的压缩优化,加密] 3 会话层 [建立应用级的连接,选择传输服务] 4 传输层 [提供不同的传输服务,流量控 ...
- linux程序对比
- list列表的使用
Python最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 list1 = [1,2,3,4,5,6,7,8,9] #创建列表 z = list([1,2,3,4,5,6,7,8 ...
- ASP.config配置
使用ASP.NET搭建三层时候, 有Model (模型)DAL(数据访问层) BLL(业务逻辑层) 连接数据库的DBhelper 放在DAL层 假如 你数据库密码改了,你要打开VS 找到DBh ...
- 使用pipenv管理你的python项目
怎么使用pipenv管理你的python项目 原文链接:https://robots.thoughtbot.com/how-to-manage-your-python-projects-with- ...
- CodeForces - 1221E Game With String 分类讨论
首先分析A能获胜的情况 A能获胜 当且仅当A拿完后所有剩下的都<b 所以一旦存在一个大小为X的 且 b<=X<a 则必是后手赢 当X为 a<=x<2*b 的时候 无论A或 ...
- wmware虚拟化的启动问题
2019-05-09,14点14 vmware出现VMware提示:已将该虚拟机配置为使用 64 位客户机操作系统.但是,无法执行 64 位操作.解决方案 进入bios里面intel 虚拟化技术 先设 ...
- .configurable:可配执行 .enumerble:枚举性 .writable:可读写性 .value:数据值
configurable:控制属性能否被删除,只有当属性的configurable特性的值为true时,该属性才能够被删除. 默认值为false,即不可删除) var person = {}; Obj ...