搜索
系统检测到您的用户名不符合规范:

漂亮的验证码

浏览:9833 发布日期:2013年08月26日 分类:系统代码 关键字: 验证码
分享一款漂亮的验证码。
此代码来源于网络,haran只是进行了简单的改写与整合。
效果
1、
2、
3、
4、
整合步骤
一、在你的项目/Lib中建立ORG目录,将TP框架/Extend/Library/ORG/Util/Image.class.php复制到刚刚建立的目录中。
二、打开刚刚复制过来的Image.class.php,将其中的buildImageVerify方法替换为以下代码 /**
+----------------------------------------------------------
* 生成图像验证码
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @param string $length 位数
* @param string $mode 类型
* @param string $type 图像格式
* @param string $width 宽度
* @param string $height 高度
* @param string $size 字体大小
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
static function buildImageVerify( $length=5, $mode='', $type='png', $width=300, $height=100, $size=40, $verifyName='verify') {

import('ORG.Util.String');
$fontPath = "./Public/elephant.ttf";
$randval = String::randString($length, $mode);
session($verifyName, md5($randval));

$width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;

if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
$im = @imagecreatetruecolor($width, $height);
} else {
$im = @imagecreate($width, $height);
}

$r = Array(225, 255, 255, 223);
$g = Array(225, 236, 237, 255);
$b = Array(225, 236, 166, 125);
$key = mt_rand(0, 3);

$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(随机)
$borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
$pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //点颜色

imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);

@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
$stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
// 干扰
for ($i = 0; $i < 10; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
for ($i = 0; $i < 25; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
}

$x = $width/$length;
for ($i = 0; $i < $length; $i++) {
//imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
imagettftext($im, $size, rand(-30,30) ,$x*$i+5, $height/1.4, $stringColor, $fontPath,$randval{$i});
}

// @imagestring($im, 5, 5, 3, $randval, $stringColor);
Image::output($im, $type);
}
三、下载elephant.ttf放置于项目目录/Public文件夹中
http://pan.baidu.com/share/link?uk=554464104&shareid=1278879446
四、建立生成验证的方法并调用<?php

Class PublicAction extends Action {

Public function verify () {
import('@.ORG.Image');
Image::buildImageVerify();
}
}

-------------------------------------以下为懒人包-----------------------------------------------------------
将附件解压到和TP框架同级的目录中

附件 haran.rar ( 42.59 KB 下载:536 次 )

评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
haran
积分:16488 等级:LV7
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /