将所有CSS加载为单个http请求和缩小形式?

时间:2014-06-02 08:11:22

标签: php html css http minify

我需要压缩我的css文件,现在加载需要很多时间,而且我还有多个http请求警告。

所以我在code.google.com上找到了这个 Code.google.com example program link

根据它们,我需要在php文件中指定所有css链接而不是我的html并在html中调用该php一次。所以http请求问题将得到解决,而google的代码也会压缩css文件。< / p>

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中添加文件???我的意思是我应该在这个php中添加css路径?

1 个答案:

答案 0 :(得分:2)

$handle = opendir(".");将读取当前目录中的所有文件。因此,只需将其更改为您的css文件即opendir("/var/www/css")

或将php文件放在css目录中