PHP-解析验证码类--学习笔记
1.开始
在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下。
2.类图
3.验证码类部分代码
3.1 定义变量
//随机因子
private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789';
private $code;
private $codeLen = 4; private $width = 130;
private $heigh = 50;
private $img;//图像 private $font;//字体
private $fontsize = 20;
$charset 是随机因子,这里是去掉了几个不容易区分的字符,如字母"i,l,o,q",数字"0,1"。有必要可以加入一些中文或其他字符或算式等。
$codeLen表示验证码长度,常见4位。
3.2构造函数,设置验证码字体,生成一个真彩色图像img
public function __construct()
{
$this->font = ROOT_PATH.'/font/Chowderhead.ttf';
$this->img = imagecreatetruecolor($this->width, $this->heigh);
}
3.3从随机因子中随机抽取4个字符,作为$code验证码.
//生成随机码
private function createCode()
{
$_len = strlen($this->charset) - 1;
for ($i = 0; $i < $this->codeLen; $i++) {
$this->code .= $this->charset[mt_rand(0, $_len)];
}
}
3.4生成验证码背景色.
//生成背景
private function createBg()
{
$color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color); }
其中mt_rand(157, 255),目的是随机取比较浅的颜色。
3.5在图像上生成文字.
//生成文字
private function createFont()
{
$_x = $this->width / $this->codeLen;
$_y = $this->heigh / 2;
for ($i = 0; $i < $this->codeLen; $i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]);
}
}
在图像上生成验证码文字,主要考虑文字在图像上的位置和每一个文字颜色。
控制第n个文字的x轴位置 = (图像宽度 / 验证码长度) * (n-1) + 随机的偏移数; 其中n = {d1....n}
控制第n个文字的y轴位置 = 图像高度 / 2 + 随机的偏移数;
mt_rand(0, 156) 随机取文字颜色,0-156目的是取比较深的颜色。
mt_rand(-30, 30) 随机的文字旋转。
3.6在图像上生成线条和雪花
//生成线条,雪花
private function createLine()
{
for ($i = 0; $i < 15; $i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color);
}
for ($i = 0; $i < 150; $i++) {
$color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color);
}
}
画线条的时候,取比较深的颜色值,而画雪花的时候取比较淡的颜色值,目的是尽可能的不影响人眼识别验证码,又能干扰自动识别验证码机制。
3.7对外生成验证码图像,供外部调用。
//对外生成
public function doImg()
{ $this->createBg(); //1.创建验证码背景
$this->createCode(); //2.生成随机码
$this->createLine(); //3.生成线条和雪花
$this->createFont(); //4.生成文字
$this->outPut(); //5.输出验证码图像
}
3.8完整代码:
<?php /**
* Created by PhpStorm.
* User: andy
* Date: 16-12-22
* Time: 下午1:20
*/
class ValidateCode
{
//随机因子
private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789';
private $code;
private $codeLen = 4; private $width = 130;
private $heigh = 50;
private $img;//图像 private $font;//字体
private $fontsize = 20; public function __construct()
{
$this->font = ROOT_PATH.'/font/Chowderhead.ttf';
$this->img = imagecreatetruecolor($this->width, $this->heigh);
} //生成随机码
private function createCode()
{
$_len = strlen($this->charset) - 1;
for ($i = 0; $i < $this->codeLen; $i++) {
$this->code .= $this->charset[mt_rand(0, $_len)];
}
} //生成背景
private function createBg()
{ $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
imagefilledrectangle($this->img, 0, $this->heigh, $this->width, 0, $color); } //生成文字
private function createFont()
{
$_x = $this->width / $this->codeLen;
$_y = $this->heigh / 2;
for ($i = 0; $i < $this->codeLen; $i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(3, 5), $_y + mt_rand(2, 4), $color, $this->font, $this->code[$i]);
}
} //生成线条,雪花
private function createLine()
{
for ($i = 0; $i < 15; $i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->heigh), mt_rand(0, $this->width), mt_rand(0, $this->heigh), $color);
}
for ($i = 0; $i < 150; $i++) {
$color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->heigh), '#', $color);
}
} //输出图像
private function outPut()
{
header('Content-Type: image/png');
imagepng($this->img);
imagedestroy($this->img);
} //对外生成
public function doImg()
{ $this->createBg(); //1.创建验证码背景
$this->createCode(); //2.生成随机码
$this->createLine(); //3.生成线条和雪花
$this->createFont(); //4.生成文字
$this->outPut(); //5.输出验证码图像
} //获取验证码
public function getCode()
{
return strtolower($this->code);
} }
class code:ValidateCode
4.测试
测试代码:
<?php
/**
* Created by PhpStorm.
* User: andy
* Date: 16-12-22
* Time: 下午1:20
*/ define('ROOT_PATH', dirname(__FILE__));
require_once ROOT_PATH.'/includes/ValidateCode.class.php'; $_vc=new ValidateCode();
echo $_vc->doImg();
生成验证码:
5.应用
<label>
<img src="../config/code.php" onclick="javascript:this.src='../config/code.php?tm='+Math.random();" />
</label>
上面onclick代码是点击验证码图片,能自动刷新验证码。
code.php:
<?php
/**
* Created by PhpStorm.
* User: andy
* Date: 16-12-22
* Time: 下午3:43
*/
require substr(dirname(__FILE__),0,-7).'/init.inc.php'; $_vc=new ValidateCode();
echo $_vc->doImg();
$_SESSION['ValidateCode']=$_vc->getCode();
有关应用的完整代码可以从https://git.oschina.net/andywww/myTest 的CMS1.0 文件里下载。
6.小结
在独立测试过程,没发现什么问题;但应用到项目的时候,刚开始发现无法生成验证码图片,网上找了一下,有的说是在outPut()函数中,
在 header('Content-Type: image/png'); 这行代码前面增加了一行ob_clean()代码,可以解决验证码这块问题。虽然此方法简单,但这可能会引起其他缓冲数据问题,因为db_clean()功能就是丢弃输出缓冲区中的内容。
PHP-解析验证码类--学习笔记的更多相关文章
- System类学习笔记
最近在学习源码的过程中发现:很多深层次的代码都用到了一个类System类,所以决定对System类一探究竟 本文先对System类进行了剖析,然后对System类做了总结 一.首先对该类的中的所有字段 ...
- zt C++ list 类学习笔记
C++ list 类学习笔记 分类: C++ 2011-09-29 00:12 7819人阅读 评论(0) 收藏 举报 listc++iteratorvectorcconstructor 双向循环链表 ...
- GUI学习之五——QAbstractButton类学习笔记
今天总结一下AbstractButton类的学习笔记. 一.描述 AbstractButton是对各种按键的抽象类他的继承关系是这样的 首先,QAbstractButton继承了QWidget类的各种 ...
- java.util.Properties类 学习笔记
学习目标: 1.认识properties文件,理解其含义,会正确创建properties文件. 2.会使用java.util.Properties类来操作properties文件. 3.掌握相对路 ...
- 在MVC中使用dotless后台动态解析LESSCSS的学习笔记
通过学习LessCSS,我们知道,Less是需要通过编译才能生成 .css 文件,主要使用三种方式进行编译: 1)使用第三方编译工具,在项目发布前编译好放在项目中. 2)在浏览器端解析执行,需要引用 ...
- JAVA与DOM解析器基础 学习笔记
要求 必备知识 JAVA基础知识.XML基础知识. 开发环境 MyEclipse10 资料下载 源码下载 文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的 ...
- 《C++ Primer Plus》10.2 抽象和类 学习笔记
10.2.1 类型是什么基本类型完成了下面的三项工作:* 决定数据对象需要的内存数量:* 决定如何解释内存中的位(long和float在内存中占用的位数相同,但是将它们转换为数值的方法不同):* 决定 ...
- C++ 类学习笔记 :: 作用域限定符
类与封装的概念: 1.类: 类的实现.类的使用. 当使用类的时候,不需要关心实现的细节.当创建类的时候,才需要考虑到内部具体的实现细节. 2.类的封装: 成员变量,C++用于表示属性的变量. 成员函数 ...
- Java常用类学习笔记总结
Java常用类 java.lang.String类的使用 1.概述 String:字符串,使用一对""引起来表示. 1.String声明为final的,不可被继承 2.String ...
随机推荐
- 使用TSQL查询和更新 JSON 数据
JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...
- In-Memory:内存优化表的事务处理
内存优化表(Memory-Optimized Table,简称MOT)使用乐观策略(optimistic approach)实现事务的并发控制,在读取MOT时,使用多行版本化(Multi-Row ve ...
- SDWebImage源码解读之SDWebImageDownloaderOperation
第七篇 前言 本篇文章主要讲解下载操作的相关知识,SDWebImageDownloaderOperation的主要任务是把一张图片从服务器下载到内存中.下载数据并不难,如何对下载这一系列的任务进行设计 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- MAC Osx PHP安装指导
php.ini的位置 Mac OS X中没有默认的php.ini文件,但是有对应的模版文件php.ini.default,位于/private/etc/php.ini.default 或者说 /etc ...
- C语言中如何判断文件是否存在
方法一:access函数判断文件夹或者文件是否存在 函数原型: int access(const char *filename, int mode); 所属头文件:io.h filename:可以填写 ...
- 关于CSS inline-block、BFC以及外边距合并的几个小问题
CSS inline-block和BCF对于初学者来说,总是弄不太明白,下面记录下我在学习这块知识的过程中遇到的几个问题,供大家参考,有不足的地方,欢迎大家批评指正. 一.在什么场景下会出现外边距合并 ...
- CentOS7之按时间段截取指定的Tomcat日志到指定文件的方法
CentOS7之按时间段截取指定的Tomcat日志到指定文件的方法 sed -n '/2016-11-02 15:00:/,/2016-11-02 15:05:/p' catalina.out > ...
- Prometheus 系统监控方案 一
最近一直在折腾时序类型的数据库,经过一段时间项目应用,觉得十分不错.而Prometheus又是刚刚推出不久的开源方案,中文资料较少,所以打算写一系列应用的实践过程分享一下. Prometheus 是什 ...
- 类型转换器(InitBinder 初始化绑定器)
单日期格式 导入jar包 创建FirstController.java @Controller public class FirstController { /** * @param binder * ...