Maison >développement back-end >tutoriel php >生成验证码图片,并输出;如何输出的总是X的图片
生成验证码图片,并输出;怎么输出的总是X的图片
$num=" ";
for($i=0;$i $num .=rand(0,9);
}
session_start();
$_SESSION["Checknum"]=$num;
header("Content-type:image/png");
srand((double)microtime()*1000000);
[email protected](60,20);
@imagecolorallocate($im,255,255,255);
$black=imagecolorallocate($im,0,0,0);
$gray=imagecolorallocate($im,200,200,200);
imagefill($im,0,0,$gray);
$style=array($black,$black,$black,$black,$black,$gray,$gray,$gray,$gray,$gray);
imagesetstyle($im,$style);
$y1=rand(0,20);
$y2=rand(0,20);
$y3=rand(0,20);
$y4=rand(0,20);
imageline($im,0,$y1,60,$y3,IMG_COLOR_STYLED);
imageline($im,0,$y2,60,$y4,IMG_COLOR_STYLED);
for($i=0;$i imagesetpixel($im,rand(0,60),rand(0,20),$black);
}
$strx=rand(3,8);
for($i=0;$i $strpos=rand(1,6);
imagestring($im,5,$strx,$strpos,substr($num,$i,1),$black);
$strx+=rand(8,12);
}
imagepng($im);
imagedestroy($im);
?>
------解决方案--------------------
1、没有加载 GD 扩展
2、程序文件有 BOM 头
------解决方案--------------------
去掉最上面的中文,session_start() 修改一下位置,似乎就可以了
session_start();
$num=" ";
for($i=0;$i $num .=rand(0,9);
}
$_SESSION["Checknum"]=$num;
header("Content-type:image/png");
srand((double)microtime()*1000000);
[email protected](60,20);
@imagecolorallocate($im,255,255,255);
$black=imagecolorallocate($im,0,0,0);
$gray=imagecolorallocate($im,200,200,200);
imagefill($im,0,0,$gray);
$style=array($black,$black,$black,$black,$black,$gray,$gray,$gray,$gray,$gray);
imagesetstyle($im,$style);
$y1=rand(0,20);
$y2=rand(0,20);
$y3=rand(0,20);
$y4=rand(0,20);
imageline($im,0,$y1,60,$y3,IMG_COLOR_STYLED);
imageline($im,0,$y2,60,$y4,IMG_COLOR_STYLED);
for($i=0;$i imagesetpixel($im,rand(0,60),rand(0,20),$black);
}
$strx=rand(3,8);
for($i=0;$i $strpos=rand(1,6);
imagestring($im,5,$strx,$strpos,substr($num,$i,1),$black);
$strx+=rand(8,12);
}
imagepng($im);
imagedestroy($im);
?>