PHP writing out PNGs, consistent problem in IE8
If I call: http://localhost/info/imgfeed.php?img=deer.png in the IE address bar then it is displayed fine.
If I link the URLs in an HTML file like this:开发者_如何学Go
<img src="imgfeed.php?img=dove.png" alt="" height="" width="" />
<img src="imgfeed.php?img=dolphin.png" alt="" height="" width="" />
<img src="imgfeed.php?img=deer.png" alt="" height="" width="" />
Then it fails miserably on IE but works fine on other browsers (tested Chrome/FF in newest stable releases). My PHP code is as follows (almost verbatim copy from PHP manual):
<?php
$server = $_SERVER['DOCUMENT_ROOT'];
$path = basename(dirname(__FILE__));
write_img($server.'/'.$path.'/'.$_GET["img"]);
function write_img($filename) {
$size = getimagesize($filename);
if ($size && $fp) {
header("Content-type: {$size['mime']}");
header("Cache-Control: no-cache");
header("Expires: -1");
readfile($filename);
exit;
} else {
// error
}
}
?>
Any ideas as to what is going on would be highly appreciated, i tried a range of tricks with headers as well as with .htaccess (making the PHP file appear as a PNG) but none of this has any effect on rendering in IE which ultimately only ever shows 3 minuscule dots (that can be saved as untitled.bmp).
Update problem solved courtesy of Meagar (remove width/height from HTML and all is good). Example above updated to reflect advice from Marc B on fpassthru vs readfile.
Have you tried omitting the width
and height
attributes for your <img>
tag? The server-side code is almost certainly fine; IE is likely interpreting width=""
as width="0"
and displaying the tiny dots you mentioned.
You could try adding some more headers which sometimes seem to fix IE issues:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
But as meagar says, IE6 doesn't like blank width and height attributes, and always requires a value in there.
have you tried using
imagepng($output);
for rendering the image?
also it can help to use
ob_clean();
flush();
before fpassthru($fp); to clean the buffer before rendering.
精彩评论