同样类似的代码,一个生成的图能正常显示,而另一个代码的图却不能显示!想了很久也不知道为什么!望高手解答,非常感谢!
此为自动生成验证码的代码:
<?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了解的人指点一二,非常感谢!

解决方案 »

  1.   

    //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);
      

  2.   

    我回答的问题都是经过测试的,绝不像某些人信口开河
    1、检查文件是否有 BOM 头
    2、检查 <?php ?> 标记外是否有空格、空行
      

  3.   

    其实楼主将第二段代码的header("content-type:image/png");注释就知道问题了
    Notice: Uninitialized string offset: 5 in D:\documentRoot\ad.php on line 26这个提示铁定会影响图片的显示的,下标过了头了,对已将循环中的<=改成<才是硬道理
      

  4.   

    谢谢回答,请问怎么验证检查文件是否有 BOM 头??用什么可以看到!上面那2个代码,我也不知道为何,现在测试又可以了。多刷新几次,就能显示出来了!
      

  5.   


    谢谢回答!现在无论用这个for($i=0;$i<=strlen($code);$i++){
    还是改为:for($i=0;$i<strlen($code);$i++){
    两个都可以正常显示,甚至我把header("content-type:image/png");注释掉,刷新显示依然没有问题!
    难道是缓冲的问题?晕了!如果是缓冲,那为何我测试其他代码,只要一改代码就能立刻起作用,只要一刷新就能产生代码效果,而用在显示图的这里,确是怎么刷新都可以!怪事了。
      

  6.   

    我在IE下,清空缓存,然后把header("content-type:image/png");注释掉,居然还能正常显示图片!我无语了!
    其实有一次测试把header("content-type:image/png");注释掉,却实会出现:
    Notice: Uninitialized string offset: 5
    这个提示!
      

  7.   

    如果是<=铁定是错的,因为你生成的字符串是5个,而你却循环6次,所以会出现“提示”,而一出现提示那么就会影响head头,铁定的没得跑
      

  8.   

    恩,大概明白你的意思了,THANKYOU!
      

  9.   

    header去掉调试,应该是有错误