php 使用压缩css文件,PHP-使用GZIP压缩静态CSS文件
所以我有一個CSS文件style.css.在同一目錄中,我有images /文件夾.
如何制作一個從另一個文件夾壓縮style.css的腳本?
現在我有這個:
if(isset($_GET['css'])) $file = array('url' => htmlspecialchars($_GET['css']), 'type' => 'text/css');
if(isset($_GET['js'])) $file = array('url' => htmlspecialchars($_GET['js']), 'type' => 'application/javascript');
if(!isset($file)) exit();
header('Content-type: '.$file['type']);
header('Cache-Control: max-age='.(60*60*6).', must-revalidate');
// whitespace+comment removal, in case gzip is off
function compress($buffer){
global $file;
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), ' ', $buffer);
return $buffer;
}
if(extension_loaded('zlib'))
if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler");
else ob_start("compress");
include($file['url']);
ob_end_flush();
?>
并使用像這樣的東西:
@import "http://mysite.com/lib/c.php?css=../style.css";
一切正常,只是css文件中圖像的路徑不再起作用.
我認為這是因為(images / bg.jpg)成為(../images/bg.jpg)
我可以解決此問題而不必將c.php腳本移動到與樣式表相同的目錄中嗎?
解決方法:
如果您的Web服務器具有此類功能,則可以使用URL重寫使內容保持整潔.
使用mod_rewrite的示例:
RewriteRule ^images/style\.css$lib/c.php?css=../style.css
然后,您可以簡單地:
@import "http://mysite.com/images/style.css";
我還建議您對腳本進行第二次查看.如果加載此URL,會發生什么?
http://mysite.com/lib/c.php?css=.htpasswd
標簽:compression,gzip,css,html,php
來源: https://codeday.me/bug/20191209/2096512.html
總結
以上是生活随笔為你收集整理的php 使用压缩css文件,PHP-使用GZIP压缩静态CSS文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle expdp 39002,e
- 下一篇: 巴菲特旗下伯克希尔-哈撒韦三季度“炒股”