为什么加上了$type = 1;$length = 4;之后 浏览网页 是一张破损的图片呢??之前没写的时候和老师的结果是一样的呀~~
2014-09-28
4 回答
<?php
require_once 'string.func.php';
//通过GD库做验证码
//创建画布
$width = 80;
$height = 30;
$image = imagecreatetruecolor($width,$height);
//用白色颜色填充画布
$white = imagecolorallocate($image,255,255,255);
//黑色画笔
$black = imagecolorallocate($image,0,0,0);
//用填充矩形填充画布
imagefilledrectangle($image,1,1,$width-2,$height-2,$white);
//生成随机字符串
$type = 1;
$length = 4;
$chars = buildRandomString($type,$length);
//设置session名字
$sess_name = "verify";
//将生成的随机字符串放于session中 便于与用户输入比对
$_SESSION[$sess_name] = $chars;
//字体
$fontfiles = array("a_d_mono.ttf","abaddon.TTF","MM.TTF");
for($i = 0;$i<$length;$i++){
//产生一个随机的的字体大小
$size = mt_rand(14,18);
//随机的角度
$angle = mt_rand(-15,15);
//产生随机的横坐标
$x = 5+$i*$size;
//产生随机的纵坐标
$y = mt_rand(20,26);
//产生随机的颜色
$color = imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));
//随机字体
$fontfile = "../fonts/".$fontfiles[mt_rand(0,count($fontfiles)-1)];
$text = substr($chars,$i,1);
imagettftext($image,$size,$angle,$x,$y);
}
header ( "content-type:image/gif" );
imagegif($image);
imagedestroy($imgage);
?>
<?php
function buildRandomString($type=1,$length=4){
if($type == 1){
$chars = join("",range(0,9));
}elseif ($type == 2){
$chars = join("",array_merge(range("a","z"),range("A","Z")));
}elseif($type ==3 ){
$chars - join("",array_merge(range(0,9),range("a","z"),range("A","Z")));
}
if($length > strlen($chars)){
exit("字符串长度不够");
}
$chars = str_shuffle($chars);
return substr($chars,0,$length);
}
?>
举报