渲染图像 php 加载缓慢 chrome
Rendered image php slow loading in chrome
我正在使用下一个代码来渲染我的图像:
// 1. Check if image exists
$image = glob("public/images/$category/$id/$name.*");
// Image exists
if(count($image) === 1) {
// 2. Get file extension
$path_parts = pathinfo($image[0]);
// 3. Add the content type to the header
switch(strtolower($path_parts['extension']))
{
case "gif":
header("Content-type: image/gif");
break;
case "jpg":
case "jpeg":
header("Content-type: image/jpeg");
break;
case "png":
header("Content-type: image/png");
break;
case "bmp":
header("Content-type: image/bmp");
break;
case "svg":
header("Content-type: image/svg+xml");
break;
default:
self::setNotFoundHeaders();
break;
}
// 4. Set the rest of the Header information
header("Expires: Mon, 1 Jan 2099 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// 5. Get the size for content length
$size= filesize($image[0]);
header("Content-Length: $size bytes");
// 6. Output the file contents
readfile($image[0]);
}
图像在 IE、Chrome 和 Firefox 中完美加载,但在 IE 和 Firefox 中加载需要 100 毫秒,而在 Chrome 中加载需要 5 秒。这是 Chrome 的网络选项卡的样子:
即使完成加载需要 5 秒,图像也可以在正常速度下准备好并在大约 100 毫秒内可见。
您还可以在图像中看到文件类型是 "document" 而不是 "image",我知道为什么。
我尝试使用不同的代码来渲染图像,但我得到了相同的行为:
$fp = fopen($image[0], 'rb');
fpassthru($fp);
我做错了什么?我错过了一些 headers 吗?
感谢您的宝贵时间!
除了 Content-Length
header 必须仅指定字节数(没有字字节)之外,您发布的代码没有任何错误。
header("Content-Length: $size");
即使有那个小错误,您的代码也应该 运行 没问题。
我有一些渲染图像的代码,我用你的 headers 尝试了它(包括 "bytes" 错误)并在 Chrome 网络工具中检查了结果。图片加载正常。
说我会尝试在 readfile()
之后添加这段代码
// Flush (if any) all the buffers
while( ob_get_level() > 0 ) { ob_end_flush(); }
// Ensure script execution terminates here
exit();
如果您仍然遇到问题,我会考虑这可能只是 Chrome 网络工具中的一个小故障。特别是因为您已经注意到图像实际上没有延迟加载。
我正在使用下一个代码来渲染我的图像:
// 1. Check if image exists
$image = glob("public/images/$category/$id/$name.*");
// Image exists
if(count($image) === 1) {
// 2. Get file extension
$path_parts = pathinfo($image[0]);
// 3. Add the content type to the header
switch(strtolower($path_parts['extension']))
{
case "gif":
header("Content-type: image/gif");
break;
case "jpg":
case "jpeg":
header("Content-type: image/jpeg");
break;
case "png":
header("Content-type: image/png");
break;
case "bmp":
header("Content-type: image/bmp");
break;
case "svg":
header("Content-type: image/svg+xml");
break;
default:
self::setNotFoundHeaders();
break;
}
// 4. Set the rest of the Header information
header("Expires: Mon, 1 Jan 2099 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// 5. Get the size for content length
$size= filesize($image[0]);
header("Content-Length: $size bytes");
// 6. Output the file contents
readfile($image[0]);
}
图像在 IE、Chrome 和 Firefox 中完美加载,但在 IE 和 Firefox 中加载需要 100 毫秒,而在 Chrome 中加载需要 5 秒。这是 Chrome 的网络选项卡的样子:
即使完成加载需要 5 秒,图像也可以在正常速度下准备好并在大约 100 毫秒内可见。
您还可以在图像中看到文件类型是 "document" 而不是 "image",我知道为什么。
我尝试使用不同的代码来渲染图像,但我得到了相同的行为:
$fp = fopen($image[0], 'rb');
fpassthru($fp);
我做错了什么?我错过了一些 headers 吗?
感谢您的宝贵时间!
除了 Content-Length
header 必须仅指定字节数(没有字字节)之外,您发布的代码没有任何错误。
header("Content-Length: $size");
即使有那个小错误,您的代码也应该 运行 没问题。
我有一些渲染图像的代码,我用你的 headers 尝试了它(包括 "bytes" 错误)并在 Chrome 网络工具中检查了结果。图片加载正常。
说我会尝试在 readfile()
// Flush (if any) all the buffers
while( ob_get_level() > 0 ) { ob_end_flush(); }
// Ensure script execution terminates here
exit();
如果您仍然遇到问题,我会考虑这可能只是 Chrome 网络工具中的一个小故障。特别是因为您已经注意到图像实际上没有延迟加载。