新开发的安全验证码类,支持生成Gif图片验证码(带噪点,干扰线,网格,随机色背景,随机自定义字体,倾斜,Gif动画)。

上图:

字体及字体文件的路径需要在类中$FontFilePath及$FontFileName中设置。如:

private static $FontFilePath = "static/font/"; //相对地本代码文件的位置
private static $FontFileName = array("3.ttf");// array("1.ttf", "2.ttf", "3.ttf", "4.ttf", "5.ttf", "6.ttf", "7.ttf", "8.ttf"); //

完整代码如下:

<?PHP

/**
说明: 验证码生成类,支持生成Gif图片验证码(带噪点,干扰线,网格,随机色背景,随机自定义字体,倾斜,Gif动画)
作者: 茶沫
时间:2013-10-26
博客:http://www.cnblogs.com/janas 服务端:
$mod = strtolower(isset($_REQUEST["mod"]) ? $_REQUEST["mod"] : "");
if($mod == "code"){
echo SecurityCode::Draw(4, 1, 120, 30, 5, 10, 100, "secode");
die();
}
调用: <img src="/getcode.php?mod=code" onclick="this.src='/getcode.php?mod=code&r='+Math.round(Math.random(0)*1000)">
验证:
$reqCode = strtolower(isset($_REQUEST["secode"]) ? $_REQUEST["secode"] : ""); //请求的验证码
$sessionCode = strtolower(isset($_SESSION["secode"]) ? $_SESSION["secode"] : ""); //会话生成的验证码
if($reqCode != $sessionCode){
echo "安全验证码错误!";
die();
}
*/
$mod = strtolower(isset($_REQUEST["mod"]) ? $_REQUEST["mod"] : "");
if ($mod == "code") {
echo SecurityCode::Draw(4, 15, 100, 27, 10, 2, 100, "secode");
die();
} //安全验证码类
class SecurityCode { private static $Debug = 0;
private static $Code = '';
private static $Chars = 'bcdefhkmnrstuvwxyABCDEFGHKMNPRSTUVWXY34568';
//private static $Chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';
private static $TextGap = 20;
private static $TextMargin = 5;
private static $FontFilePath = "static/font/"; //相对地本代码文件的位置
private static $FontFileName =array("3.ttf");// array("1.ttf", "2.ttf", "3.ttf", "4.ttf", "5.ttf", "6.ttf", "7.ttf", "8.ttf"); //
private static $Img = 'GIF89a'; //GIF header 6 bytes
private static $BUF = Array();
private static $LOP = 0;
private static $DIS = 2;
private static $COL = -1;
private static $IMG = -1; /**
生成GIF图片验证
@param int $L 验证码长度
@param int $F 生成Gif图的帧数
@param int $W 宽度
@param int $H 高度
@param int $MixCnt 干扰线数
@param int $lineGap 网格线间隔
@param int $noisyCnt 澡点数
@param int $sessionName 验证码Session名称
*/
public static function Draw($L = 4, $F = 1, $W = 150, $H = 30, $MixCnt = 2, $lineGap = 0, $noisyCnt = 10, $sessionName = "Code") {
ob_start();
ob_clean(); for ($i = 0; $i < $L; $i++) {
self::$Code .= SubStr(self::$Chars, mt_rand(0, strlen(self::$Chars) - 1), 1);
} if (!isset($_SESSION))
session_start();
$_SESSION[$sessionName] = strtolower(self::$Code); $bgRGB = array(rand(0, 255), rand(0, 255), rand(0, 255));
//生成一个多帧的GIF动画
for ($i = 0; $i < $F; $i++) {
$img = ImageCreate($W, $H); //背景色
$bgColor = imagecolorallocate($img, $bgRGB[0], $bgRGB[1], $bgRGB[2]);
ImageColorTransparent($img, $bgColor);
unset($bgColor); //添加噪点
$maxNoisy = rand(0, $noisyCnt);
$noisyColor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
for ($k = 0; $k <= $maxNoisy; $k++) {
imagesetpixel($img, rand(0, $W), rand(0, $H), $noisyColor);
} //添加网格
if ($lineGap > 0) {
for ($m = 0; $m < ($W / $lineGap); $m++) { //竖线
imageline($img, $m * $lineGap, 0, $m * $lineGap, $H, $noisyColor);
}
for ($n = 0; $n < ($H / $lineGap); $n++) { //横线
imageline($img, 0, $n * $lineGap, $W, $n * $lineGap, $noisyColor);
}
}
unset($noisyColor); // 添加干扰线
for ($k = 0; $k < $MixCnt; $k++) {
$wr = mt_rand(0, $W);
$hr = mt_rand(0, $W);
$lineColor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
imagearc($img, $W - floor($wr / 2), floor($hr / 2), $wr, $hr, rand(90, 180), rand(180, 270), $lineColor);
unset($lineColor);
unset($wr, $hr);
} //第一帧忽略文字
if ($i != 0 || $F <= 1) {
//文字
$foreColor = imagecolorallocate($img, rand(0, 255), rand(0, 255), rand(0, 255));
for ($j = 0; $j < $L; $j++) {
$fontFile = self::$FontFilePath . self::$FontFileName[rand(0, count(self::$FontFileName) - 1)];
if (!file_exists($fontFile))
imagestring($img, 4, self::$TextMargin + $j * self::$TextGap, ($H - rand($H / 2, $H)), self::$Code[$j], $foreColor);
else
imageTTFtext($img, rand(15, 18), rand(-15, 15), self::$TextMargin + $j * self::$TextGap, ($H - rand(7, 10)), $foreColor, $fontFile, self::$Code[$j]);
}
unset($foreColor);
} ImageGif($img);
Imagedestroy($img);
$Imdata[] = ob_get_contents();
OB_clean();
} unset($W, $H, $B);
if (self::$Debug) {
echo $_SESSION['code'];
echo '<pre>', Var_Dump($Imdata), '</pre>';
die();
}
header('Content-type:image/gif');
return self::CreateGif($Imdata, 20);
unset($Imdata);
} private static function CreateGif($GIF_src, $GIF_dly = 10, $GIF_lop = 0, $GIF_dis = 0, $GIF_red = 0, $GIF_grn = 0, $GIF_blu = 0, $GIF_mod = 'bin') {
if (!is_array($GIF_src) && !is_array($GIF_tim)) {
throw New Exception('Error:' . __LINE__ . ',Does not supported function for only one image!!');
die();
}
self::$LOP = ($GIF_lop > -1) ? $GIF_lop : 0;
self::$DIS = ($GIF_dis > -1) ? (($GIF_dis < 3) ? $GIF_dis : 3) : 2;
self::$COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ? ($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;
for ($i = 0, $src_count = count($GIF_src); $i < $src_count; $i++) {
if (strToLower($GIF_mod) == 'url') {
self::$BUF[] = fread(fopen($GIF_src[$i], 'rb'), filesize($GIF_src[$i]));
} elseif (strToLower($GIF_mod) == 'bin') {
self::$BUF[] = $GIF_src[$i];
} else {
throw New Exception('Error:' . __LINE__ . ',Unintelligible flag (' . $GIF_mod . ')!');
die();
}
if (!(Substr(self::$BUF[$i], 0, 6) == 'GIF87a' Or Substr(self::$BUF[$i], 0, 6) == 'GIF89a')) {
throw New Exception('Error:' . __LINE__ . ',Source ' . $i . ' is not a GIF image!');
die();
}
for ($j = (13 + 3 * (2 << (ord(self::$BUF[$i]{10}) & 0x07))), $k = TRUE; $k; $j++) {
switch (self::$BUF[$i]{$j}) {
case '!':
if ((substr(self::$BUF[$i], ($j + 3), 8)) == 'NETSCAPE') {
throw New Exception('Error:' . __LINE__ . ',Could not make animation from animated GIF source (' . ($i + 1) . ')!');
die();
}
break;
case ';':
$k = FALSE;
break;
}
}
}
self::AddHeader();
for ($i = 0, $count_buf = count(self::$BUF); $i < $count_buf; $i++) {
self::AddFrames($i, $GIF_dly);
}
self::$Img .= ';';
return (self::$Img);
} private static function AddHeader() {
$i = 0;
if (ord(self::$BUF[0]{10}) & 0x80) {
$i = 3 * (2 << (ord(self::$BUF[0]{10}) & 0x07));
self::$Img .= substr(self::$BUF[0], 6, 7);
self::$Img .= substr(self::$BUF[0], 13, $i);
self::$Img .= "!\377\13NETSCAPE2.0\3\1" . chr(self::$LOP & 0xFF) . chr((self::$LOP >> 8) & 0xFF) . "\0";
}
unset($i);
} private static function AddFrames($i, $d) {
$L_str = 13 + 3 * (2 << (ord(self::$BUF[$i]{10}) & 0x07));
$L_end = strlen(self::$BUF[$i]) - $L_str - 1;
$L_tmp = substr(self::$BUF[$i], $L_str, $L_end);
$G_len = 2 << (ord(self::$BUF[0]{10}) & 0x07);
$L_len = 2 << (ord(self::$BUF[$i]{10}) & 0x07);
$G_rgb = substr(self::$BUF[0], 13, 3 * (2 << (ord(self::$BUF[0]{10}) & 0x07)));
$L_rgb = substr(self::$BUF[$i], 13, 3 * (2 << (ord(self::$BUF[$i]{10}) & 0x07)));
$L_ext = "!\xF9\x04" . chr((self::$DIS << 2) + 0) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";
if (self::$COL > -1 && ord(self::$BUF[$i]{10}) & 0x80) {
for ($j = 0; $j < (2 << (ord(self::$BUF[$i]{10}) & 0x07)); $j++) {
if (ord($L_rgb{3 * $j + 0}) == (self::$COL >> 0) & 0xFF && ord($L_rgb{3 * $j + 1}) == (self::$COL >> 8) & 0xFF && ord($L_rgb{3 * $j + 2}) == (self::$COL >> 16) & 0xFF) {
$L_ext = "!\xF9\x04" . chr((self::$DIS << 2) + 1) . chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . chr($j) . "\x0";
break;
}
}
}
switch ($L_tmp{0}) {
case '!':
$L_img = substr($L_tmp, 8, 10);
$L_tmp = substr($L_tmp, 18, strlen($L_tmp) - 18);
break;
case ',':
$L_img = substr($L_tmp, 0, 10);
$L_tmp = substr($L_tmp, 10, strlen($L_tmp) - 10);
break;
}
if (ord(self::$BUF[$i]{10}) & 0x80 && self::$IMG > -1) {
if ($G_len == $L_len) {
if (self::Compare($G_rgb, $L_rgb, $G_len)) {
self::$Img .= ($L_ext . $L_img . $L_tmp);
} else {
$byte = ord($L_img{9});
$byte |= 0x80;
$byte &= 0xF8;
$byte |= (ord(self::$BUF[0]{10}) & 0x07);
$L_img{9} = chr($byte);
self::$Img .= ($L_ext . $L_img . $L_rgb . $L_tmp);
}
} else {
$byte = ord($L_img{9});
$byte |= 0x80;
$byte &= 0xF8;
$byte |= (ord(self::$BUF[$i]{10}) & 0x07);
$L_img{9} = chr($byte);
self::$Img .= ($L_ext . $L_img . $L_rgb . $L_tmp);
}
} else {
self::$Img .= ($L_ext . $L_img . $L_tmp);
}
self::$IMG = 1;
} private static function Compare($G_Block, $L_Block, $Len) {
for ($i = 0; $i < $Len; $i++) {
if ($G_Block{3 * $i + 0} != $L_Block{3 * $i + 0} || $G_Block{3 * $i + 1} != $L_Block{3 * $i + 1} || $G_Block{3 * $i + 2} != $L_Block{3 * $i + 2}) {
return (0);
}
}
return (1);
} }

Gif图片验证码类的更多相关文章

  1. PHP编写的图片验证码类文件分享方法

    适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...

  2. PHP:现有图片验证码类

    文章来源:http://www.cnblogs.com/hello-tl/p/7593022.html <?php class TL_Captcha_img{ private $image; / ...

  3. 图片验证码的JAVA工具类

    我们平时开发时经常会遇到需要图片验证码,基础的验证码包括了数字.字母.甚至可能有汉字.下面我给出一个简单的工具类. package com..ankang.tony.util; import java ...

  4. 开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...

  5. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

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

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

  7. android图片验证码--自绘控件

    自绘控件的内容都是自己绘制出来的 大致流程如下: 1.定义一个类继承view 使用TypedArray初始化属性集合 在view的构造方法中 有一个AttributeSet的参数 很明显是用来保存控件 ...

  8. THINKPHP源码学习--------验证码类

    TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...

  9. ThinkPHP 3.2.3 加减乘法验证码类

    ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Ver ...

随机推荐

  1. 转:.NET特性与反射

    .NET编译器的任务之一是为所有定义和引用的类型生产元数据描述.除了程序集中标准的元数据外,.NET平台允许程序员使用特性(attribute)把更多的元数据嵌入到程序集中.简而言之,特性就是用于类型 ...

  2. html-2, a img ul li ol dl dt dd 标签与列表标签的简单使用

    <!-- a:  a{ /*清除a标签的下划线*/ text-decoration: none; }  (1)超链接 href 超链接的地址 target: _self 默认 在当前中打开链接地 ...

  3. Spring Data Jpa示例(IntelliJ maven项目)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例,(本示例中省略了业务逻辑组件UserService) 2. 在pom.xml中配置依赖 包括: spring-context spri ...

  4. 笔记1:Jmeter工作原理及目录结构

    1.基本工作原理 发送request请求到服务器——获取目标服务的统计信息——生成不同格式的报告 2.完整的工作原理 Jmeter模拟用户并发进行性能测试——发送request到目标服务器——服务器返 ...

  5. DNS域名解析的配置

    /etc/resolv.conf它是DNS客户机配置文件,用于设置DNS服务器的IP地址及DNS域名,还包含了主机的域名搜索顺序.该文件是由域名解析 器(resolver,一个根据主机名解析IP地址的 ...

  6. Mybatis导入原生配置文件

    在Spring-Mybatis中导入Mybatis原生配置文件 在sqlSessionFactory Bean中设置设置configLocation属性 <property name=" ...

  7. [转]Markdown 公式指导手册(包含LaTeX)

    Cmd Markdown 公式指导手册 本文为转载文章,并且由于LaTeX的可能不能全部兼容,所以可能有部分公式无法在博客园显示,可以移步原网站. 本文固定链接: https://www.zybulu ...

  8. Android中APK安装过程及原理解析

    [原文] 来自华为内部资料 应用安装是智能机的主要特点,即用户可以把各种应用(如游戏等)安装到手机上,并可以对其进行卸载等管理操作.APK是Android Package的缩写,即android安装包 ...

  9. Javascript -- 示例:多选下拉选框

    1. 示例:多选下拉选框 <html> <head> <meta http-equiv="Content-Type" content="te ...

  10. win7 vmware虚拟机上网设置

    1.上网方式设成HOST-ONLY 2.将主机的网络共享VMnet1(完成第一步设置后,VMware自动分配虚拟网络VMnet1) 3.win7下查看VMnet1网络ip 4.根据3查看的IP地址在v ...