doujiaci7976 2012-12-21 08:14
浏览 62
已采纳

无法创建/显示php图像 - GD图像损坏或截断

I'm having a problem and i'd like some help please. I have made a script for creating an image with PHP using GD library, but the problem is when i preview it on the Firefox it shows me an error in Firebug: "Image corrupt or truncated:localhost/my_scripts/images.php". NOTE: this is not an error displayed from PHP, it seems more like an error comming from the browser.

I have checked my code for syntaxes many times, i have also tried out and other gd functions, like imagegif, imagettftext etc etc but didn't have any effect. Here's my code:

//$image = @imagecreate(200, 20)or die("Cannot Initialize new GD image stream");
$image = imagecreate(200, 20);
$background = imagecolorallocate($image,0,0,0);
$foreground = imagecolorallocate($image,255,255,255);
imagestring($image,5,5,1,"This is a Test",$foreground);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);

GD library is enabled i've already checked that in phpinfo(), and I have also tried to preview it and on Chrome and looks the same. Any ideas what i might doing wrong or why happens this? and how can i fix this error??

  • 写回答

2条回答 默认 最新

  • duanban4769 2012-12-23 08:37
    关注

    I found something about this error and finally i figured out what was going on, so here is the solution to the problem:

    Make sure that PHP code is the only thing on the page. No HTML, no spaces before or after the PHP code, etc, the only thing it should send to the browser should be the image data. If that still doesn't work then make sure that saving the PHP script in a format that does not include the UTF BOM and this should fix it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题