大量PHP生成的图像加载缓慢


Slow loading with big amount of PHP generated images

我的自动生成图像有问题。

我有PHP脚本,它获取图像的url哈希,并通过url中的"m"参数以请求的维度和修改生成此图像。它可以改变尺寸,诸如此类的东西。

只有一张或几张照片都很棒,比如:http://www.jaarda.eu/upload/pluginGallery/129b12182490d9e191b3a7cdd4fcf704?m=wh_400-400

但在很大程度上,我有问题,没有加载一些项目。检查http://www.jaarda.eu/.

我不认为,这是服务器的问题,CPU不能达到最大值,内存也不能。脚本,它生成图像,甚至发送缓存标题,但仍然没有改变-在页面上有大量的图片,如jaarda.eu是个问题。当脚本启动时,并不是每次都生成图片的其他维度——图片只生成一次,然后保存在服务器上供再次使用。我甚至尝试多次增加我的VPS的功率,但没有改变。

这个问题没有在Safari中显示,只有IE、Chrome等…

这是我的脚本的结尾,这里是功能缓存和类型发送:

$modified = filemtime($file);
            if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
                $request_modified = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
                if ($modified <= $request_modified) {
                    //header('HTTP/1.1 304 Not Modified');
                    header('HTTP/1.1 304 Not Modified', true, 304);
                }
            }           
            header("Last-Modified: ".gmdate('D, d M Y H:i:s', $modified)." GMT");
            header("Cache-Control: public, max-age=7200");
            header("Expires: " . gmdate('D, d M Y H:i:s', time()+7200) . ' GMT');           
            header('Content-Length: ' . filesize($file));
            header('Content-Type: '.$type);
            readfile($file);

非常感谢你的想法,如何解决这个问题。

编辑

我已经通过在代码中使用ob_函数来改善我的情况,但它仍然不完美。更好的是,只有两个图像中的一个没有加载,但仍然不完美。问题仍然存在于ctrl+f5重新加载的chrome中,所以感谢大家的更多想法。

$modified = filemtime($file);
        ob_start(""); 
        if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
            $request_modified = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
            if ($modified <= $request_modified) {
                //header('HTTP/1.1 304 Not Modified');
                header('HTTP/1.1 304 Not Modified', true, 304);
            }
        }           
        header("Last-Modified: ".gmdate('D, d M Y H:i:s', $modified)." GMT");
        header("Cache-Control: public, max-age=7200");
        header("Expires: " . gmdate('D, d M Y H:i:s', time()+7200) . ' GMT');           
        header('Content-Length: ' . filesize($file));
        header('Content-Type: '.$type);
        ob_end_clean();
        ob_clean(); 
        flush();
        readfile($file);
        exit(); 

您应该为m=wh_400-400添加一些尺寸验证,因为我确实尝试了一些其他尺寸,如wh=3000-3000,它很有效,您知道如果您存储这些图像,这意味着什么。

要回答您的问题,您应该尝试使用htaccess:

# BEGIN Expire headers
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 7200 seconds"
    ExpiresByType image/jpg "access plus 2592000 seconds"
    ExpiresByType image/jpeg "access plus 2592000 seconds"
    ExpiresByType image/png "access plus 2592000 seconds"
    ExpiresByType image/gif "access plus 2592000 seconds"
    AddType image/x-icon .ico
    ExpiresByType image/ico "access plus 2592000 seconds"
    ExpiresByType image/icon "access plus 2592000 seconds"
    ExpiresByType image/x-icon "access plus 2592000 seconds"
    ExpiresByType text/css "access plus 2592000 seconds"
    ExpiresByType text/javascript "access plus 2592000 seconds"
    ExpiresByType text/html "access plus 7200 seconds"
    ExpiresByType application/xhtml+xml "access plus 7200 seconds"
    ExpiresByType application/javascript A259200
    ExpiresByType application/x-javascript "access plus 2592000 seconds"
    ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
</IfModule>
# END Expire headers
# BEGIN Cache-Control Headers
<IfModule mod_headers.c>
    <FilesMatch "''.(ico|jpe?g|png|gif|swf|gz|ttf)$">
        Header set Cache-Control "max-age=2592000, public"
    </FilesMatch>
    <FilesMatch "''.(css)$">
        Header set Cache-Control "max-age=2592000, public"
    </FilesMatch>
    <FilesMatch "''.(js)$">
        Header set Cache-Control "max-age=2592000, private"
    </FilesMatch>
    <filesMatch "''.(html|htm)$">
        Header set Cache-Control "max-age=7200, public"
    </filesMatch>
    #Disable caching for scripts and other dynamic files
    <FilesMatch "'.(pl|php|cgi|spl|scgi|fcgi)$">
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
        #Header merge Cache-Control no-cache env=CGI
        #Header merge Cache-Control no-cache env=NO_CACHE
        #Header merge Cache-Control no-store env=NO_STORE
    </FilesMatch>
</IfModule>
# END Cache-Control Headers

我从控制台得到的错误是:资源被解释为图像,但使用MIME类型text/html/传输

所以问题是,你的var$类型是从哪里来的?试着调试你发送的内容,我相信你"发送"了错误的内容,导致浏览器延迟。