PHP,IIS图像无法在浏览器上显示

时间:2010-01-23 06:44:57

标签: php iis image-processing

以下代码未显示IIS 6 / PHP 5.2.9中的图像。它可以在XAMPP(PHP 5.3)

中正常工作
$img = @imagecreate(200, 200);
$background_color = imagecolorallocate($img, 0, 0, 0);
$text_color = imagecolorallocate($img, 233, 14, 91);
imagestring($img, 12, 60, 90,  'image here', $text_color);
header('Last-Modified: ' . date('D, d M Y H:i:s')); 
header('Content-type: image/jpg');
header('Content-Disposition: inline; filename=blank_jpeg.jpg'); 
ob_start();
    imagejpeg($img);
    imagedestroy($img);
    $jpeg = ob_get_contents();
ob_end_clean();
header ('Content-length: ' . strlen($jpeg));
echo $jpeg;
exit;

2 个答案:

答案 0 :(得分:0)

很可能没有为IIS正确安装GD。

打开error loggingspecify an error log并在调用脚本时检查错误。

答案 1 :(得分:0)

我终于找到了错误的根源。这段代码是在Wordpress上运行的插件中。现在在Wordpress社区中,有一种技术用于在基于IIS的系统上启用pretty-urls,这些系统没有一些处理mod_rewrite的适当方法。该技术涉及捕获由pretty-urls引起的404错误并通过index.php重新路由页面。

事实证明,在此之前的某个地方,在输出流中插入了大约3个字符()其他页面输出。这对于html页面来说不是问题,因为字符是不可见的,浏览器只是忽略它们。但是,在图像的情况下,二进制数据的格式是关键的。因此,在图像数据之前插入这三个字符会导致无法识别的字节序列被浏览器拒绝,因为它需要一个图像。

在我的特定情况下,解决方案是启用mod_rewrite处理,问题就消失了。我希望这些信息可以节省数小时的调试时间。