您当前的位置:首页 > CMS常见问题 > 帝国cms常见问题 >

自定义验证码图片的宽高后文本垂直水平居中[帝国cms ShowKey.php]

0
<?php
define('EmpireCMSAdmin','1');
define('EmpireCMSAPage','login');
define('EmpireCMSNFPage','1');
require('../class/connect.php');
//绘制居中文本, 对于GD的内置字体
function pc_ImageStringCenter($image,$text,$font){
	//字体大小
	$width = array(1=>5,6,7,8,9);
	$height = array(1=>6,8,13,15,15);
	//计算图像大小
	$xi = imagesx($image);
	$yi = imagesy($image);
	//计算文本大小
	$xr = $width[$font] * strlen($text);
	$yr = $height[$font];
	//计算中心点的位置
	$x=intval(($xi-$xr)/2);
	$y=intval(($yi-$yr)/2);
	return array($x,$y);
}
//取得随机数
function domake_password($pw_length){
	global $public_r;
	//验证码字符组成,0数字,1字母,2数字+字母
	if($public_r["keytog"]==1)
	{
		$low_ascii_bound=65;
		$upper_ascii_bound=90;
		$notuse=array(73,79);
	}
	elseif($public_r["keytog"]==2)
	{
		$low_ascii_bound=50;
		$upper_ascii_bound=90;
		//过滤一些特殊符号和容易看错的字母
		//58(:) 59(;) 60(<) 61(=) 62(>) 63(?) 64(@) 73(I) 76(L) 79(O)
		$notuse=array(58,59,60,61,62,63,64,73,76,79);
	}
	else
	{
		$low_ascii_bound=48;
		$upper_ascii_bound=57;
		$notuse=array(48,49);
	}
	while($i<$pw_length){
		if(PHP_VERSION<"4.2.0")
		{
			mt_srand((double)microtime()*1000000);
		}
		$randnum=mt_rand($low_ascii_bound,$upper_ascii_bound);
		if(!in_array($randnum,$notuse))
		{
			$password1=$password1.chr($randnum);
			$i++;
		}
	}
	return $password1;
}
//返回颜色
function ReturnShowKeyColor($img){
	global $public_r;
	//背景色
	if($public_r["keybgcolor"])
	{
		$bgcr=ToReturnRGB($public_r["keybgcolor"]);
		$r["bgcolor"]=imagecolorallocate($img,$bgcr[0],$bgcr[1],$bgcr[2]);
	}
	else
	{
		$r["bgcolor"]=imagecolorallocate($img,7,27,63);
	}
	//文字色
	if($public_r["keyfontcolor"])
	{
		$fcr=ToReturnRGB($public_r["keyfontcolor"]);
		$r["fontcolor"]=ImageColorAllocate($img,$fcr[0],$fcr[1],$fcr[2]);
	}
	else
	{
		$r["fontcolor"]=ImageColorAllocate($img,255,255,255);
	}
	//干扰色
	if($public_r["keydistcolor"])
	{
		$dcr=ToReturnRGB($public_r["keydistcolor"]);
		$r["distcolor"]=ImageColorAllocate($img,$dcr[0],$dcr[1],$dcr[2]);
	}
	else
	{
		$r["distcolor"]=ImageColorAllocate($img,255,255,255);
	}
	return $r;
}
//显示验证码
function ShowKey($v){
	$vname=ecmsReturnKeyVarname($v);
	//取得四位随机验证码
	$key=strtolower(domake_password(4));
	//设置验证码
	ecmsSetShowKey($vname,$key,0);
	//是否支持gd库
	if(function_exists("imagegif"))
	{
		header("Content-type:image/gif");
		$img=imagecreate(60,32);
		$colorr=ReturnShowKeyColor($img);
		$bgcolor=$colorr["bgcolor"];
		$fontcolor=$colorr["fontcolor"];
		$distcolor=$colorr["distcolor"];
		imagefill($img,0,0,$bgcolor);
		list($x,$y)=pc_ImageStringCenter($img,$key,5);
		imagestring($img,5,$x,$y,$key,$fontcolor);
		//for($i=0;$i<90;$i++)
		//{
			//imagesetpixel($img,rand()%70,rand()%30,$distcolor);
		//}
		imagegif($img);
		imagedestroy($img);
	}
	elseif(function_exists("imagepng"))
	{
		header("Content-type:image/png");
		$img=imagecreate(50,26);
		$colorr=ReturnShowKeyColor($img);
		$bgcolor=$colorr["bgcolor"];
		$fontcolor=$colorr["fontcolor"];
		$distcolor=$colorr["distcolor"];
		imagefill($img,0,0,$bgcolor);
		imagestring($img,5,6,3,$key,$fontcolor);
		for($i=0;$i<90;$i++)
		{
			imagesetpixel($img,rand()%70,rand()%30,$distcolor);
		}
		imagepng($img);
		imagedestroy($img);
	}
	elseif(function_exists("imagejpeg"))
	{
		header("Content-type:image/jpeg");
		$img=imagecreate(50,26);
		$colorr=ReturnShowKeyColor($img);
		$bgcolor=$colorr["bgcolor"];
		$fontcolor=$colorr["fontcolor"];
		$distcolor=$colorr["distcolor"];
		imagefill($img,0,0,$bgcolor);
		imagestring($img,5,6,3,$key,$fontcolor);
		for($i=0;$i<90;$i++)
		{
			imagesetpixel($img,rand()%70,rand()%30,$distcolor);
		}
		imagejpeg($img);
		imagedestroy($img);
	}
	elseif(function_exists("imagewbmp")) 
	{
		header("Content-type:image/vnd.wap.wbmp");
		$img=imagecreate(50,26);
		$colorr=ReturnShowKeyColor($img);
		$bgcolor=$colorr["bgcolor"];
		$fontcolor=$colorr["fontcolor"];
		$distcolor=$colorr["distcolor"];
		imagefill($img,0,0,$bgcolor);
		imagestring($img,5,6,3,$key,$fontcolor);
		for($i=0;$i<90;$i++)
		{
			imagesetpixel($img,rand()%70,rand()%30,$distcolor);
		}
		imagewbmp($img);
		imagedestroy($img);
	}
	else
	{
		ecmsSetShowKey($vname,"ecms",0);
		echo ReadFiletext("../data/images/ecms.jpg");
	}
}
//返回变量名
function ecmsReturnKeyVarname($v){
	$name="checkkey";
	return $name;
}
$v=$_GET["v"];
ShowKey($v);
?>

建站咨询

在线咨询真诚为您提供专业解答服务

咨询热线

137 1731 25507×24小时服务热线

微信交流

二维码终于等到你,还好我没放弃
返回顶部