简体   繁体   English

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

[英]PHP, IIS Image not displaying on browser

The following code is not displaying the image from IIS 6/PHP 5.2.9. 以下代码未显示IIS 6 / PHP 5.2.9中的图像。 It works OK from XAMPP (PHP 5.3) 从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;

More likely than not GD is not installed correctly for IIS. 对于IIS,很可能没有正确安装GD。

Turn on error logging , specify an error log and check it for errors when calling the script. 打开错误日志记录指定错误日志并在调用脚本时检查错误。

I finally tracked the error to its roots. 我终于找到了错误的根源。 This code was in a plugin running on Wordpress. 这段代码在Wordpress上运行的插件中。 Now in the Wordpress community, there is a technique used to enable pretty-urls on IIS based systems that do not have some appropriate way of processing mod_rewrite. 现在,在Wordpress社区中,有一种技术可用于在没有某些适当方式处理mod_rewrite的基于IIS的系统上启用漂亮的URL。 The technique involves capturing 404 errors caused by the pretty-urls and re-routing the page through index.php. 该技术涉及捕获由漂亮网址引起的404错误,并通过index.php重新路由页面。

It turns out that somewhere along the way some 3 characters ( ï , » and ¿ ) are inserted in the output stream before any other page output. 事实证明,在输出任何其他页面之前,在输出流中插入了大约3个字符(  )。 This is not a problem for html pages since the characters are invisible and the browser just ignores them. 对于html页面而言,这不是问题,因为字符是不可见的,浏览器只会忽略它们。 However, in the case of images, the format of the binary data is critical. 但是,对于图像,二进制数据的格式至关重要。 So inserting those three characters ahead of the image data results in an unrecognizable sequence of bytes that gets rejected by the browser since it expects an image. 因此,在图像数据之前插入这三个字符会导致无法识别的字节序列,由于期望图像,浏览器会拒绝它。

The solution in my particular case was to enable mod_rewrite handling and the problem disappeared. 在我的特殊情况下,解决方案是启用mod_rewrite处理,问题消失了。 I hope this info saves someone hours of debugging. 我希望此信息可以节省一些调试时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM