PHP 验证码:扭曲+粘连+变形
一,绪论
由于项目需要,需要加强目前的验证码,我们参照的对象是支付宝。
基于PHP CodeIgniter 框架,代码放置在下面的路径下。
/application/libraries
二,主要代码
class VerifyCode
{ //声明图像大小
private $width = 78;
private $height = 46; //验证码字符有限集
private $v_char = '1234567890';
private $v_code_str = ''; //验证码数量
private $v_num = 4; // 第i个文字x轴起始位置计算公式: x轴起始坐标 = margin + padding * i
//文字内外边距
private $padding = 15;
private $margin = 3; //字体大小
private $font_size = 30; //字体逆时针旋转的角度
private $font_angles = array(-5, 5); //字体名称
//private $font = 'Wattauchimma.ttf';
private $font = 'msyh.ttf'; //加上路径非常重要 //图像容器
private $img; //颜色容器
private $colors = array(); /**
* 生成图片验证码主逻辑
* @author 冯煜博
*/
public function __construct()
{
//生成一幅图像
$this->img = imagecreate($this->width, $this->height); //生成颜色
$this->colors['white'] = imagecolorallocate($this->img, 255,255,255);
$this->colors['blue'] = imagecolorallocate($this->img, 0, 47, 167); // 生成纯白色背景
imagecolorallocate($this->img, 255,255,255); // 设置GD库环境变量
putenv('GDFONTPATH=' . realpath('.')); //生成验证码字符
$this->randomContent();
} /**
* 输出验证码,返回值是验证码的字符串表示
* @author 冯煜博
* @return string
*/
public function show()
{
$this->generate(); header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header("content-type: image/png"); ImagePNG($this->img);
ImageDestroy($this->img); return $this->v_code_str;
} /**
* 生成随机的验证码的内容
* @author 冯煜博
* @return string
*/
private function randomContent()
{
for($i = 0; $i < $this->v_num; $i++)
{
$this->v_code_str .= $this->v_char[ rand(0, strlen($this->v_char) - 1)];
}
} /**
* 生成验证码的图像
* @author 冯煜博
*/
private function generate()
{
//生成验证码的算法
for($i = 0; $i < $this->v_num; $i++)
{
// 下一个字符的起始x轴坐标
$x = $this->margin + $this->padding * $i;
// 下一个字符的起始y轴坐标
$y = 38; imagettftext(
$this->img,
$this->font_size,
$this->font_angles[ rand(0, count($this->font_angles) - 1) ],
$x, $y,
$this->colors['blue'],
APPPATH.'libraries/'.$this->font, //加上了字体的相对路径
$this->v_code_str[ $i ]
);
} $dst = imagecreatetruecolor($this->width, $this->height);
$dWhite = imagecolorallocate($dst, 255, 255, 255);
imagefill($dst,0,0,$dWhite); //扭曲,变形
for($i = 0; $i < $this->width; $i++)
{
// 根据正弦曲线计算上下波动的posY $offset = 4; // 最大波动几个像素
$round = 2; // 扭2个周期,即4PI
$posY = round(sin($i * $round * 2 * M_PI / $this->width ) * $offset); // 根据正弦曲线,计算偏移量 imagecopy($dst, $this->img, $i, $posY, $i, 0, 1, $this->height);
} $this->img = $dst;
} public function __destruct()
{
unset($this->colors);
}
}
三,CI 框架内的写法
比如在 VCode 控制器内的 show 方法中,调用:
class VCode extends CI_Controller
{
/*
* 显示验证码的网页实际上是异步进行加载的,也就是先后发起两次请求。
* 第一次加载HTML页面;
* 第二次加载图片
*/
public function show()
{ $this->load->library('VerifyCode'); $this->load->library('session');
$this->session->set_flashdata('vcode', $this->verifycode->show()); //session_start();
//$_SESSION['vcode'] = $this->verifycode->show();
} }
四,THML调用代码
我们知道,只要访问 {$domain}index.php/vcode/show 就可以看到一张验证码图片。
所以在HTML的 img src属性中填写上面的URL就可以。
<html>
<body> <img src="index.php/vcode/show" /> <br/> <?php echo form_open('vcode/verify') ?>
<input name="codeStr" />
<input type="submit" name="submit" value="verify" />
</form> </body>
</html>
五,生成的验证码效果图

PHP 验证码:扭曲+粘连+变形的更多相关文章
- java web 验证码-数字不变形
controller代码: import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.a ...
- Pyhthon爬虫其之验证码识别
背景 现在的登录系统几乎都是带验证手段的,至于验证的手段也是五花八门,当然用的最多的还是验证码.不过纯粹验证码识已经是很落后的东西了,现在比较多见的是滑动验证,滑动拼图验证(这个还能往里面加广告).点 ...
- 字符型图片验证码识别完整过程及Python实现
字符型图片验证码识别完整过程及Python实现 1 摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...
- Atitit 图片 验证码生成attilax总结
Atitit 图片 验证码生成attilax总结 1.1. 图片验证码总结1 1.2. 镂空文字 打散 干扰线 文字扭曲 粘连2 1.1. 图片验证码总结 因此,CAPTCHA在图片验证码这一应用点 ...
- Python识别字符型图片验证码
前言 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越来越严峻.本文介绍了一套字符验证码识别的完整流程,对于验 ...
- 完整的验证码识别流程基于svm(若是想提升,可优化)
字符型图片验证码识别完整过程及Python实现 首先很感觉这篇文章的作者,将这篇文章写的这么好.我呢,也是拿来学习,觉得太好,所以忍不住就进行了转载. 因为我个人现在手上也有个验证码识别的项目,只是难 ...
- python 验证码 高阶验证
python 验证码 高阶验证 标签: 验证码python 2016-08-19 15:07 1267人阅读 评论(1) 收藏 举报 分类: 其他(33) 目录(?)[+] 字符型图片验证 ...
- 字符识别Python实现 图片验证码识别
字符型图片验证码识别完整过程及Python实现 1 摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...
- ArcGIS之Cartogram地图变形记
一.地图会说谎 地图作为真实世界的抽象,是“用图说话”最可靠的工具,但是有的时候地图也会撒一些小小的谎言,其中最著名的例子当属美国总统大选.如图1是2012年美国总统大选后网上给出的一个结果图,红色代 ...
随机推荐
- SQL中的四种语言DDL,DML,DCL,TCL
1.DDL(Data Definition Language)数据库定义语言statements are used to define the database structure or schema ...
- go语言编程入门
查看文档 首先先分享一个可以在本地就能查看文档的骚操作(linux系统) 1.打开命令行终端,输入godoc -http=:8000,如果想后台运行在后面加个& 2.然后打开浏览器,输入网址: ...
- Java中子类是否可以继承父类的static变量和方法而呈现多态特性
静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法,关于static方法,声明为static的方法有以下几条限制: 它们仅能调用其他的static 方法. 它 ...
- matplotlib基本使用方法
[微语]人生有可为之事,也有不可为之事.可为之事,当尽力为之,此谓尽性,不可为之事,当尽心为之,此谓知命. 三人行必有我师 官方参考API:https://matplotlib.org/tutoria ...
- Intel 设计缺陷背后的原因是什么? | Linux 中国
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/F8qG7f9YD02Pe/article/details/79386769 wx_fmt=jpeg& ...
- PHP Warning: preg_match(): JIT compilation failed: no more memory in
PHP7.3出现如下错误:PHP Warning: preg_match(): JIT compilation failed: no more memory in ... 解决方案: 修改 /usr/ ...
- 循环结构 for
for格式:for(初始化表达式;循环条件表达式;循环后的操作表达式) { 执行语句:循环体 } ------------------------------------ -------------- ...
- windows使用git时出现:warning: LF will be replaced by CRLF的解决办法
在Windows环境下使用git进行add的时候,会提示如下warning: “warning:LF will be replacee by CRLF”. 这是因为在Windows中的换行符为CRLF ...
- PAT 1097 Deduplication on a Linked List[比较]
1097 Deduplication on a Linked List(25 分) Given a singly linked list L with integer keys, you are su ...
- [LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...