开发者

PHP, IIS Image not displaying on browser

开发者 https://www.devze.com 2022-12-18 01:42 出处:网络
The following code is not displaying the image from IIS 6/PHP 5.2.9. It works OK from XAMPP (PHP 5.3)

The following code is not displaying the image from IIS 6/PHP 5.2.9. It works OK from 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);
    im开发者_如何学编程agedestroy($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.

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. 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. The technique involves capturing 404 errors caused by the pretty-urls and re-routing the page through 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. This is not a problem for html pages since the characters are invisible and the browser just ignores them. 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. I hope this info saves someone hours of debugging.

0

精彩评论

暂无评论...
验证码 换一张
取 消