<?php
class SecureCode
{
private static $instance=null;
private $code = '';
private $fontFile;
private $validate;
private $image;
private $specialAdd = 'special string for SecureCode';
private $codeExpire=86400;
private $codeCookieName='secure_code';
/**
* 构造方法
*/
private function SecureCode()
{
$this->fontFile = dirname( __FILE__ ) . '/arial.ttf';
}
private function __construct()
{
$this->SecureCode();
}
public static function getInstance()
{
if (self::$instance==null)
self::$instance=new self();
return self::$instance;
}
/**
* 指定字体文件所在路径,默认为当前文件夹下arial.ttf文件
* @param $fontFile 文件路径
* @return void
*/
function loadFont($fontFile)
{
$this->fontFile = $fontFile;
}
/**
* 图片输出方法,在执行本方法前程序不应该有任何形式的输出
* @return void;
*/
function stroke()
{
$this->saveCode();
self::sendHeader();
imagegif( $this->validate );
imagedestroy( $this->validate );
imagedestroy( $this->image );
}
/**
* 图片保存方法
* @param $fileName 保存路径
* @return void
*/
function save($fileName)
{
$this->saveCode();
imagegif( $this->validate , $fileName );
imagedestroy( $this->validate );
imagedestroy( $this->image );
}
/**
* 验证码验证方法
* @param $input 要验证的字符串,即用户的输入内容
* @return boolean 验证结果
*/
function verify($input)
{
$input=strtolower($input);
$targetCode=$this->authCode($input);
$code=$this->getCookie();
if (empty($code)||$code!=$targetCode)
$result= false;
else
$result=true;
$_COOKIE[$this->codeCookieName]='';
setcookie ( $this->codeCookieName, '', - 1 );
return $result;
}
/**
* 图片创建方法
* @return void;
*/
function createImage()
{
$this->randCode();
$size = 30;
$width = 90;
$height = 35;
$degrees = array (
rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 )
);
for ($i = 0; $i < 4; ++$i)
{
if (rand() % 2);
else $degrees[$i] = -$degrees[$i];
}
$this->image = imagecreatetruecolor( $size , $size );
$this->validate = imagecreatetruecolor( $width , $height );
$back = imagecolorallocate( $this->image , 255 , 255 , 255 );
$border = imagecolorallocate( $this->image , 0 , 0 , 0 );
imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
for ($i = 0; $i < 4; ++$i)
{
$temp = self::RgbToHsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
if ($temp[2] > 60) $temp[2] = 60;
$temp = self::HsvToRgb( $temp[0] , $temp[1] , $temp[2] );
$textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
}
for ($i = 0; $i < 200; ++$i)
{
$randpixelcolor = ImageColorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
}
$temp = self::RgbToHsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
if ($temp[2] < 200) $temp[2] = 255;
$temp = self::HsvToRgb( $temp[0] , $temp[1] , $temp[2] );
$randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
putenv( 'GDFONTPATH=' . realpath( '.' ) );
// Name the font to be used (note the lack of the .ttf extension
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontFile , $this->code[0] );
$this->image = imagerotate( $this->image , $degrees[0] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontFile , $this->code[1] );
$this->image = imagerotate( $this->image , $degrees[1] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontFile , $this->code[2] );
$this->image = imagerotate( $this->image , $degrees[2] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontFile , $this->code[3] );
$this->image = imagerotate( $this->image , $degrees[3] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
}
/**
* 获取随机生成的验证码
* @return string 随机验证码,返回的验证码不进行任何处理
*/
function getCode()
{
return $this->code;
}
/**
* 生成随机码方法
* @return void;
*/
protected function randCode()
{
$alphastr = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
$randStr = array (
$alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}
);
$this->code = strtolower( $randStr[0] . $randStr[1] . $randStr[2] . $randStr[3] );
}
/**
* RGB色到HSV色转变方法
* @param $R
* @param $G
* @param $B
* @return array HSV数组
*/
protected static function RgbToHsv($R, $G, $B)
{
$tmp = min( $R , $G );
$min = min( $tmp , $B );
$tmp = max( $R , $G );
$max = max( $tmp , $B );
$V = $max;
$delta = $max - $min;
if ($max != 0) $S = $delta / $max; // s
else
{
$S = 0;
//$H = UNDEFINEDCOLOR;
return;
}
if ($R == $max) $H = ($G - $B) / $delta; // between yellow & magenta
else if ($G == $max) $H = 2 + ($B - $R) / $delta; // between cyan & yellow
else $H = 4 + ($R - $G) / $delta; // between magenta & cyan
$H *= 60; // degrees
if ($H < 0) $H += 360;
return array (
没有合适的资源?快使用搜索试试~ 我知道了~
PHP验证码类 包括图片生成和验证
共2个文件
php:1个
ttf:1个
4星 · 超过85%的资源 需积分: 9 31 下载量 67 浏览量
2010-10-15
16:38:09
上传
评论 1
收藏 196KB ZIP 举报
温馨提示
一个PHP生成验证码图片以及进行验证的类 实例如下: 调用实例如下 require 'SecureCode.class.php'; $code=SecureCode::getInstance(); $code->createImage(); $code->stroke(); /* 如果需要保存验证码图片,将$code->stroke()改为$code->save($file); */ 验证验证码的时候,调用代码如下 require 'SecureCode.class.php'; $userInput=$_POST['user_input_code']; $code=SecureCode::getInstance(); $code->verify($userInput);
资源推荐
资源详情
资源评论
收起资源包目录
验证码类.zip (2个子文件)
SecureCode.class.php 11KB
arial.ttf 359KB
共 2 条
- 1
资源评论
- lx659789302012-08-13缺少示例,还望更新个实例就好了!
- liyueleeonbbbaaa2012-07-28没有说明调用方法...>..
huaihuajio
- 粉丝: 19
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功