渲染图像php在chrome中加载缓慢
我正在使用下一个代码渲染图像:渲染图像php在chrome中加载缓慢,php,image,google-chrome,header,render,Php,Image,Google Chrome,Header,Render,我正在使用下一个代码渲染图像: // 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 he
// 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”,idk why
我尝试使用不同的代码渲染图像,但得到了相同的行为:
$fp = fopen($image[0], 'rb');
fpassthru($fp);
我做错了什么?我是否缺少一些标题
谢谢你抽出时间 您发布的代码中没有错误,除了
内容长度
标题必须只指定字节数(不包括单词bytes)
即使有那个小错误,您的代码也应该运行良好
我有一些渲染图像的代码,我用你的确切标题(包括“字节”错误)进行了尝试,并在Chrome web工具中检查了结果。图像加载正常
说我会在readfile()之后再尝试一件事添加此代码
如果你仍然遇到这个问题,我会认为这只是Chrome Web工具中的一个故障。特别是因为您已经注意到图像实际上是无延迟加载的。
您发布的代码中没有任何错误,除了内容长度
标题必须只指定字节数(不包括字字节)
即使有那个小错误,您的代码也应该运行良好
我有一些渲染图像的代码,我用你的确切标题(包括“字节”错误)进行了尝试,并在Chrome web工具中检查了结果。图像加载正常
说我会在readfile()之后再尝试一件事添加此代码
如果你仍然遇到这个问题,我会认为这只是Chrome Web工具中的一个故障。特别是因为你已经注意到图像实际上已加载,没有延迟。
谢谢@paolowhile(ob_get_level()>0){ob_end_flush()}
。当我在等待答案时,我一直在尝试一些东西,这个标题也会在图像显示后停止加载:header(“Connection:close”)
但是在文档中说,Connection header中列出的消息标题不能包括端到端标题,例如缓存控制。
所以它可能与我使用的缓存控制头idk冲突。。。我应该使用刷新缓冲区还是添加上一个标头?再次感谢您的时间只需刷新缓冲区并退出()谢谢@paolowhile(ob_get_level()>0){ob_end_flush()}
。当我在等待答案时,我一直在尝试一些东西,这个标题也会在图像显示后停止加载:header(“Connection:close”)
但是在文档中说,Connection header中列出的消息标题不能包括端到端标题,例如缓存控制。
所以它可能与我使用的缓存控制头idk冲突。。。我应该使用刷新缓冲区还是添加上一个标头?再次感谢您的时间刷新缓冲区并退出()
header("Content-Length: $size");
// Flush (if any) all the buffers
while( ob_get_level() > 0 ) { ob_end_flush(); }
// Ensure script execution terminates here
exit();