我的验证码显示不出来,是一堆乱码,但是好像前几个字符不变,求问怎么回事?
<?php
require_once 'string.func.php';
//通过GD库做验证码
function varifyImage($sess_name="varify",$type=1,$length=4,$pixel=0,$line=0){
session_start();
$width=80;
$height=28;
$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);//用填充矩阵填充画布
$chars=buildRandomString($type,$length);//string.func.php中产生随机字符串的函数
$_SESSION[$sess_name]=$chars;//用于比对用户输入是否正确
$fontfile="../fonts/SIMYOU.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));
$text=substr($chars,$i,1);//从产生的验证码中按顺序取一位
//以上产生随机的字符大小、角度、横坐标、纵坐标、颜色
imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
}
if ($pixel) { //加干扰点
$color = imagecolorallocate ( $image, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
for($i = 0; $i < $pixel; $i ++) {
imagesetpixel ( $image, mt_rand( 0, $width-1), mt_rand( 0, $height-1), $black);
}
}
if ($line) { //加干扰线
for($i = 0;$i<$line; $i ++) {
$color = imagecolorallocate($image,mt_rand(0,255 ),mt_rand(0,255 ),mt_rand(0, 255));
imageline($image,mt_rand(0, $width-1), mt_rand ( 0, $height-1), mt_rand ( 0, $width-1), mt_rand ( 0, $height-1), $color );
}
}
header("content_type:image/gif");//告诉浏览器要显示什么类型的图片
imagegif($image);//显示画布
imagedestroy($image);//销毁画布
}
varifyImage();
?>代码是照着老师打的(ಥ _ ಥ)
我把你的代码拷贝试了一下, 是最后那句header()里面的代码书写有误, 是content-type , 你写成了content_type, 改过了就可以了, 我试了, 可以正常显示验证码了