您的位置:首页 > 编程语言 > PHP开发

PHP验证码实现类可生成图片

2015-05-19 11:56 309 查看
PHP验证码实现类

<?php
/*
|+-------------------------------------------------------------------------
| PHP验证码实现类
|+-------------------------------------------------------------------------
| 功能:
|  可自定义验证码内容、自定义验证码长度、自定义验证码生成的宽度和高度;
|  可生成图片形式,须图片的存放路径,并保证目录可写,可设置定时删除验证码图片
|  可设置干扰元素:支持 点点、竖线的干扰
|  可设置验证码字体,可设置多种随机字体,可设置每个验证码使用不同的字体
|+-------------------------------------------------------------------------
| Author  stillwater
| Wechat  stillwater1024   Email:stillwater1024@qq.com
| Create date 2015-3-31
|+-------------------------------------------------------------------------
*/

/**
* 验证码实现类(开启GD库)
* @access    public
* @param array   data  必须是一个数据
* @param string  img_path 生成验证码图片的存储路径
* @param string  img_url 验证码图片的使用路径
* @param string  font_path 字体路径
* @param array   font_files 字体库
* @return    string
*/
function create_captcha ($data = '',$img_path = '', $img_url = '', $font_path = '',$font_files='')
{
// -----------------------------------
// 默认参数
// -----------------------------------
$default = array(
'word'=>'',
'length'=>'4',
'img_url' => '',
'img_path' => '',
'img_width' => '90',
'img_height' => '30',
'type'=>'1',
'font_path'=>'',
'font_files'=>'',
'point'=>1,
'line'=>5,
'border_color'=>1,
'expiration' => 7200
);

// -----------------------------------
// $data 有值则使用,无值则使用默认值
// 同时把key  转换成变量 直接使用
// -----------------------------------
foreach ($default as $key=>$val)
{
if (!is_array($data))
{

if (!isset($$key) || $$key =='')
{
$$key = $val;
}
}else
{
$$key = isset($data[$key])?$data[$key]:$val;
}

}

//是否开启GD库
if (! extension_loaded('gd'))
{
return FALSE;
}

// -----------------------------------
// 生成图片的条件下 先删除
// -----------------------------------
if ($img_path!='' && @is_dir($img_path) && is_writable($img_path))
{
list($usec, $sec) = explode(" ", microtime());
$now = ((float)$usec + (float)$sec);

$current_dir = @opendir($img_path);

while ($filename = @readdir($current_dir))
{
if ($filename != "." and $filename != ".." and $filename != "index.html")
{
$name = str_replace(".gif", "", $filename);
//设置 每隔多久删除一次验证码图片
if (($name + $expiration) < $now)
{
@unlink($img_path.$filename);
}
}
}

@closedir($current_dir);
}

// -----------------------------------
// 验证码类型  可单独封装成函数
// -----------------------------------

if ($word=='')
{
if ($type == 1)
{
//0-9
$pool = join("",range(0,9));
}else if ($type ==2)
{
// a-z  A-Z
$pool = join("",array_merge(range("a","z"),range("A","Z")));
}else if ($type ==3)
{
// 0-9 a-z  A-Z
$pool  = join("",array_merge(range(0,9),range("a","z"),range("A","Z")));
}

$length = isset($data['length'])?intval($data['length']):$length;

$str = '';
for ($i = 0; $i<$length;$i++)
{
$str .= substr($pool,mt_rand(0,strlen($pool) - 1),1);
}

$word = $str;

}

//随机打乱字符,生成最终验证码
$word  = str_shuffle($word);

// -----------------------------------
// 确定角度和位置
// -----------------------------------
$length = strlen($word);
$angle  = ($length >=6)?rand(-($length-6),($length-6)):0;
$x_axis = rand(6,(360/$length)-16);
$y_axis = ($angle >=0)?rand($img_height,$img_width):rand(6,$img_height);

// -----------------------------------
// 创建画布
// -----------------------------------

if (function_exists('imagecreatetruecolor'))
{
$im = imagecreatetruecolor($img_width, $img_height);
}else
{
$im = imagecreate($img_width, $img_height);
}

// -----------------------------------
// 设置背景颜色
// -----------------------------------
$bg_color       = imagecolorallocate ($im, 255, 255, 255);
$border_color   = imagecolorallocate ($im, 153, 102, 102);
$text_color     = imagecolorallocate ($im, 204, 153, 153);
$grid_color     = imagecolorallocate($im, 255, 182, 182);
$shadow_color   = imagecolorallocate($im, 255, 240, 240);
$rand_color = imagecolorallocate ( $im, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );

// -----------------------------------
//  填充矩形画布
// -----------------------------------
ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);

// -----------------------------------
//  设置每个字体样式 ,支持多种字体
// -----------------------------------

$use_font = ($font_path !='' && is_dir($font_path) && !empty($font_files) && function_exists('imagettftext'))? TRUE:FALSE;

//设置每个字体的样式
for ($j=0;$j<$length;$j++)
{
//字体随机大小、角度、位置
$font_size = mt_rand ( 12, 24 );
$x = 5 + $j * $font_size;
$y = mt_rand ( 20, 26 );

if ($use_font == FALSE)
{
$y = rand(0 , $img_height/2);
imagestring($im, $font_size, $x, $y, substr($word, $j, 1), $text_color);
$x += ($font_size*2);
}else
{
$y = rand($img_height/2, $img_height-3);
imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $j, 1));
$x += $font_size;
}
}

// -----------------------------------
//  设置点点 干扰元素
// -----------------------------------
if ($point)
{
for ($i= 0;$i <50;$i++)
{
imagesetpixel($im,mt_rand ( 0, $img_width - 1 ), mt_rand ( 0, $img_height - 1 ),$text_color);
}
}

// -----------------------------------
//  设置竖线 干扰元素
// -----------------------------------
if($line)
{
for ($i=1;$i<$line;$i++)
{
imageline($im,mt_rand ( 0, $img_width - 1 ), mt_rand ( 0, $img_height - 1 ),mt_rand ( 0, $img_width - 1 ), mt_rand ( 0, $img_height - 1 ),$rand_color);
}
}

// -----------------------------------
//  创建验证码边框
// -----------------------------------
if ($border_color)
{
imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
}

// -----------------------------------
//  是否生成验证码图片
// -----------------------------------

if ($img_path!='' && @is_dir($img_path) && is_writable($img_path))
{
$img_name = $now.'.gif';
ImageGIF($im, $img_path.$img_name);

$img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
ImageDestroy ($im );
return array('word' => $word, 'time' => $now, 'image' => $img);
}else
{

header( "Content-type:image/gif" );
ImageGIF($im);
ImageDestroy ($im);
return $word;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: