PHP - 使用GZIP压缩静态css文件 [英] PHP - compress static css file with GZIP

查看:105
本文介绍了PHP - 使用GZIP压缩静态css文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个css文件style.css。在同一个目录中我有图像/文件夹。
如何制作压缩style.css文件的脚本,但是从另一个文件夹中获取?



现在我拥有了这个功能:

 <?php 
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');

//如果gzip关闭,whitespace +注释删除
函数compress($ buffer){
global $ file;
$ buffer = preg_replace('!/ \ * [^ *] * \ * +([^ /] [^ *] * \ * +)* /!','',$ buffer) ;
$ buffer = str_replace(array(\r\\\
,\r,\\\
,\t,'','',''),'' ,$ buffer);
返回$ buffer;

$ b $ if(extension_loaded('zlib'))
if(substr_count($ _ SERVER ['HTTP_ACCEPT_ENCODING'],'gzip'))ob_start(ob_gzhandler);

else ob_start(compress);

包括($ file ['url']);
ob_end_flush();
?>

并使用如下所示:

 < style type =text / css> 
@importhttp://mysite.com/lib/c.php?css=../style.css;
< / style>

除了css文件中的图像路径不再工作以外,一切正常。
i认为这是因为(images / bg.jpg)变成(../ images / bg.jpg)

我可以解决这个问题,而不必移动我的c .php脚本与样式表位于同一目录?

解决方案

如果您的web服务器具有此功能,可以使用URL重写



以mod_rewrite为例:

  RewriteRule ^ images / style \.css $ lib / c.php?css = .. / style.css 

然后,您可以简单地:

  @importhttp://mysite.com/images/style.css; 

我也建议你再看看你的脚本。如果我加载这个网址会发生什么?

  http://mysite.com/lib/c.php?css=.htpasswd 


so I have a css file, style.css. in the same directory I have the images/ folder. How can I make a script that compresses style.css, but from another folder?

Right now I have this:

<?php
  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();
?>

and use it something like this:

<style type="text/css">
 @import "http://mysite.com/lib/c.php?css=../style.css";
</style>

everything is ok except that the path to the images inside the css file don't work anymore. i think it's because (images/bg.jpg) becomes (../images/bg.jpg)

can i fix this without having to move my c.php script in the same directory as the stylesheet?

解决方案

If your web server has such feature, you can use URL rewriting to keep things tidy.

An example with mod_rewrite:

RewriteRule ^images/style\.css$ lib/c.php?css=../style.css

Then, you can simply:

@import "http://mysite.com/images/style.css";

I also recommend you give a second look to your script. What happens if I load this URL?

http://mysite.com/lib/c.php?css=.htpasswd

这篇关于PHP - 使用GZIP压缩静态css文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆