加载所有css作为一个单一的http请求和缩小的形式


Load all css as a single http request and minified form?

我需要压缩我的css文件,目前它需要很多时间来加载,我也得到了多个http请求警告。

所以我在code.google.com上找到了这个示例程序链接

根据他们,我需要指定所有的CSS链接在一个PHP文件,而不是我的HTML和调用PHP一次在HTML。因此,http请求问题将得到解决,从谷歌的代码压缩css文件也。

Instead of
link rel="stylesheet" type="text/css" href="/css/main.css" />
link rel="stylesheet" type="text/css" href="/css/menu.css" />
link rel="stylesheet" type="text/css" href="/css/blog.css" />
link rel="stylesheet" type="text/css" href="/css/box.css" />
link rel="stylesheet" type="text/css" href="/css/form.css" />
link rel="stylesheet" type="text/css" href="/css/profile.css" />
link rel="stylesheet" type="text/css" href="/css/page.css" />
link rel="stylesheet" type="text/css" href="/css/gallery.css" />
Your write
link rel="stylesheet" type="text/css" href="/css/css_include.php" />

这是php代码格式

<?php
function compress($buffer) 
{
    $buffer = preg_replace('!/'*[^*]*'*+([^/][^*]*'*+)*/!', '', $buffer);
    $buffer = str_replace(array("'r'n", "'r", "'n", "'t", '  ', '    ', '    '), '', $buffer);
    return $buffer;
}
$handle = opendir(".");
header  ('Content-type: text/css');
header  ("Cache-control: public");
header  ("Vary: Accept-Encoding");
header  ("Content-Encoding: gzip");
ob_start("ob_gzhandler");
while (false != ($file = readdir($handle))) 
{
    if (strpos($file,".css") !== FALSE)
    {
        echo compress (file_get_contents($file));
    }
}
?>

但是我的问题是我如何在php中添加文件??我的意思是我应该在哪里添加css路径在这个php?

$handle = opendir(".");将读取当前目录下的所有文件。所以把它改成css文件所在的位置也就是opendir("/var/www/css")

或者将php文件放到css目录