gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:33:42 27 4
gpt4 key购买 nike

我需要压缩我的 css 文件,目前加载需要很多时间,而且我还收到多个 http 请求警告。

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

根据他们的说法,我需要在 php 文件而不是我的 html 中指定所有 css 链接,并在 html 中调用该 php 一次。因此 http 请求问题将得到解决,来自 google 的代码也会压缩 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 中添加文件???我的意思是我应该在这个 php 中的什么位置添加 css 路径?

最佳答案

$handle = opendir("."); 会读取当前目录下的所有文件。所以只需将其更改为您的 css 文件所在的位置,即 opendir("/var/www/css")

或者将php文件放在你的css目录下

关于php - 将所有 css 加载为单个 http 请求和缩小形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23989983/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com