简体   繁体   English

渲染图像php缓慢加载铬

[英]Rendered image php slow loading in chrome

I am using the next code to render my images: 我正在使用下一个代码来渲染我的图像:

// 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]);
}

The image loads perfectly in IE, Chrome and Firefox but while it takes 100 ms to load in IE and Firefox, it takes 5 seconds to load in Chrome. 图像在IE,Chrome和Firefox中完美加载,但在IE和Firefox中加载需要100毫秒,在Chrome中加载需要5秒。 This is what the network tab of Chrome looks like: 这就是Chrome的网络标签看起来像:

铬网络

Even if it takes 5 seconds to finish loading, the image is ready and visible at normal speed, in about 100 ms. 即使完成加载需要5秒钟,图像也准备好并以正常速度显示,大约100毫秒。

Also you can see in the image that the file type is "document" and not "image", idk why. 您还可以在图像中看到文件类型是“文档”而不是“图像”,idk为什么。

I have tried using different code to render the image but I got the same behavior: 我尝试使用不同的代码来渲染图像,但我得到了相同的行为:

$fp = fopen($image[0], 'rb');
fpassthru($fp);

What am I doing wrong? 我究竟做错了什么? Am I missing some headers? 我错过了一些标题吗?

Thank you for your time! 感谢您的时间!

There is nothing wrong in the code you posted except that the Content-Length header must specify only the number of bytes (without the word bytes). 您发布的代码没有任何错误,只是Content-Length标头必须只指定字节数(没有字节字节)。

header("Content-Length: $size");

Even with that minor mistake however your code should run fine. 即使有这个小错误,你的代码应运行正常。

I have some code that render images, I tried it with your exact headers ("bytes" mistake included) and inspected the result in Chrome web tools. 我有一些渲染图像的代码,我尝试使用您的确切标题(包括“字节”错误)并在Chrome网络工具中检查结果。 The images are loaded normally. 图像正常加载。

Said that I would try one more thing adding this code after readfile() 说我会再尝试在readfile()之后添加这段代码

// Flush (if any) all the buffers
while( ob_get_level() > 0 ) { ob_end_flush(); }

// Ensure script execution terminates here
exit();

If you still experience the problem I would consider the possibility that it's just a glitch in Chrome web tools. 如果您仍然遇到此问题,我会考虑这可能只是Chrome网络工具中的一个小问题。 Expecially because you've noticed the image is actually loaded without delays. 特别是因为你已经注意到图像实际加载没有延迟。

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

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