一,绪论

  由于项目需要,需要加强目前的验证码,我们参照的对象是支付宝。

  基于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 验证码:扭曲+粘连+变形的更多相关文章

  1. java web 验证码-数字不变形

    controller代码: import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.a ...

  2. Pyhthon爬虫其之验证码识别

    背景 现在的登录系统几乎都是带验证手段的,至于验证的手段也是五花八门,当然用的最多的还是验证码.不过纯粹验证码识已经是很落后的东西了,现在比较多见的是滑动验证,滑动拼图验证(这个还能往里面加广告).点 ...

  3. 字符型图片验证码识别完整过程及Python实现

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

  4. Atitit 图片 验证码生成attilax总结

    Atitit 图片 验证码生成attilax总结 1.1. 图片验证码总结1 1.2. 镂空文字  打散 干扰线 文字扭曲 粘连2 1.1. 图片验证码总结 因此,CAPTCHA在图片验证码这一应用点 ...

  5. Python识别字符型图片验证码

    前言 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越来越严峻.本文介绍了一套字符验证码识别的完整流程,对于验 ...

  6. 完整的验证码识别流程基于svm(若是想提升,可优化)

    字符型图片验证码识别完整过程及Python实现 首先很感觉这篇文章的作者,将这篇文章写的这么好.我呢,也是拿来学习,觉得太好,所以忍不住就进行了转载. 因为我个人现在手上也有个验证码识别的项目,只是难 ...

  7. python 验证码 高阶验证

    python 验证码 高阶验证 标签: 验证码python 2016-08-19 15:07 1267人阅读 评论(1) 收藏 举报  分类: 其他(33)    目录(?)[+]   字符型图片验证 ...

  8. 字符识别Python实现 图片验证码识别

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

  9. ArcGIS之Cartogram地图变形记

    一.地图会说谎 地图作为真实世界的抽象,是“用图说话”最可靠的工具,但是有的时候地图也会撒一些小小的谎言,其中最著名的例子当属美国总统大选.如图1是2012年美国总统大选后网上给出的一个结果图,红色代 ...

随机推荐

  1. poj1269 intersecting lines【计算几何】

    We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...

  2. HDU 4352 - XHXJ's LIS - [数位DP][LIS问题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  3. POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]

    嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...

  4. 与虚拟机连接出现ora-12514错误解决方法

    谈到ora-12154问题,网上有一大堆解决方法,原因基本统一:tns或listener配置不正确.对于listener配置不正确的一般较少发生,大多数人都是按照默认配置一路“下一步”过来的,基本都是 ...

  5. 自定义vueawesomeswiper分页器样式

    swiperOption: {//swiper的配置项 notNextTick: true,//想获得swiper实例对象,这个必须为true direction: 'vertical', // gr ...

  6. LeetCode 第 338 题 (Counting Bits)

    Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...

  7. 网站/IIS/Web/WCF服务 访问共享目录 映射 的解决方案

    目录 问题案例 原因分析 解决问题 总结 问题案例 环境: 电脑A:winform程序: 电脑B:部署了一个文件上传的WCF服务在IIS上.且该服务的配置文件中已经增加 <identity im ...

  8. 登录mysql出现/var/lib/mysql/mysql.sock不存在

    问题描述: 1.mysql安装完成后,使用 service mysqld start 总是出现 start failed. 2.使用mysql -uroot -p登录出现找不到 /var/lib/my ...

  9. [dt]世纪历史长河年代表

    年代口诀 夏商与西周, 东周分两段, 春秋和战国, 一统秦两汉, 三分魏蜀吴, 二晋前后延, 南北朝并列, 隋唐五代传, 宋元明清后, 皇朝至此完. 中国历史长河年代表 参考: 中国历史朝代顺序表.年 ...

  10. (转)How to Use Elasticsearch, Logstash, and Kibana to Manage MySQL Logs

    A comprehensive log management and analysis strategy is vital, enabling organizations to understand ...