同样类似的代码,一个生成的图能正常显示,而另一个代码的图却不能显示!想了很久也不知道为什么!望高手解答,非常感谢!
此为自动生成验证码的代码:
<?php
header("content-type:image/png");
$code = strtoupper(substr(md5(rand()),0,5));
$nimg=imagecreatetruecolor(200,30);
imagecolorallocate($nimg,255,255,255);
for($i=0;$i<=strlen($code);$i++){
$font=mt_rand(2,4);
$x=mt_rand(1,8);
$y=mt_rand(1,4)+30*$i/10;
$color=imagecolorallocate($nimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
//imagesetpixel($nimg, rand()%70 , rand()%30 , $color);
imagestring($nimg,$font,$x,$y,$code[$i],$color);
}
imagepng($nimg);
imagedestroy($nimg);
?>
这个代码在页面上显示为:一个很小的交叉,查看属性为;超文本传送协议不可用,
而另外一个代码就能正常显示:
<?php
header("content-type:image/png");
$nimg=imagecreatetruecolor(500,500);
$color=imagecolorallocate($nimg,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagefill($nimg,250,250,250);
imagerectangle($nimg,5,20,350,350,$color);
imageline($nimg,5,20,350,350,$color);
imagestring($nimg,5,250,250,'php5',$color);
imagepng($nimg);
imagedestroy($nimg);
?>
此显示正常!
望对gd2了解的人指点一二,非常感谢!
此为自动生成验证码的代码:
<?php
header("content-type:image/png");
$code = strtoupper(substr(md5(rand()),0,5));
$nimg=imagecreatetruecolor(200,30);
imagecolorallocate($nimg,255,255,255);
for($i=0;$i<=strlen($code);$i++){
$font=mt_rand(2,4);
$x=mt_rand(1,8);
$y=mt_rand(1,4)+30*$i/10;
$color=imagecolorallocate($nimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
//imagesetpixel($nimg, rand()%70 , rand()%30 , $color);
imagestring($nimg,$font,$x,$y,$code[$i],$color);
}
imagepng($nimg);
imagedestroy($nimg);
?>
这个代码在页面上显示为:一个很小的交叉,查看属性为;超文本传送协议不可用,
而另外一个代码就能正常显示:
<?php
header("content-type:image/png");
$nimg=imagecreatetruecolor(500,500);
$color=imagecolorallocate($nimg,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagefill($nimg,250,250,250);
imagerectangle($nimg,5,20,350,350,$color);
imageline($nimg,5,20,350,350,$color);
imagestring($nimg,5,250,250,'php5',$color);
imagepng($nimg);
imagedestroy($nimg);
?>
此显示正常!
望对gd2了解的人指点一二,非常感谢!
$code = strtoupper(substr(md5(rand()),0,5));
$nimg=imagecreatetruecolor(200,30);
imagecolorallocate($nimg,255,255,255);
for($i=0;$i<=strlen($code);$i++){//改成<
$font=mt_rand(2,4);
$x=mt_rand(1,8);
$y=mt_rand(1,4)+30*$i/10;
$color=imagecolorallocate($nimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
//imagesetpixel($nimg, rand()%70 , rand()%30 , $color);
imagestring($nimg,$font,$x,$y,$code[$i],$color);
}
imagepng($nimg);
imagedestroy($nimg);
1、检查文件是否有 BOM 头
2、检查 <?php ?> 标记外是否有空格、空行
Notice: Uninitialized string offset: 5 in D:\documentRoot\ad.php on line 26这个提示铁定会影响图片的显示的,下标过了头了,对已将循环中的<=改成<才是硬道理
谢谢回答!现在无论用这个for($i=0;$i<=strlen($code);$i++){
还是改为:for($i=0;$i<strlen($code);$i++){
两个都可以正常显示,甚至我把header("content-type:image/png");注释掉,刷新显示依然没有问题!
难道是缓冲的问题?晕了!如果是缓冲,那为何我测试其他代码,只要一改代码就能立刻起作用,只要一刷新就能产生代码效果,而用在显示图的这里,确是怎么刷新都可以!怪事了。
其实有一次测试把header("content-type:image/png");注释掉,却实会出现:
Notice: Uninitialized string offset: 5
这个提示!