dongliehuan3925 2011-07-05 13:32
浏览 22
已采纳

缩小动态php样式表

So I have been using Minify to compress my JS and CSS which had all been going nicely until I needed to compress some dynamic php stylesheets.

I tried to use htaccess to fool it into thinking it was a css file, but I then realised it uses absolute file paths which would not be effected by mod_rewrite

Anyways whenever I point it at a php file, it always returns '400 Bad Request'. Any idea on how to solve this other than writing my own compression script?

  • 写回答

1条回答 默认 最新

  • dr5648 2011-07-05 13:53
    关注

    I find the best way to deal with minifying and compressing stylesheets is to do it yourself. Check out this code:

    <?php
    header("Content-Type: text/css");
    header("Last-Modified: ".gmdate('D, d M Y H:i:s', filemtime($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']))." GMT");
    header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']) + 691200))." GMT");
    
    //This GZIPs the CSS for transmission to the user
    //making file size smaller and transfer rate quicker
    ob_start("ob_gzhandler");
    ob_start("compress");
    
    //Function for compressing the CSS as tightly as possible
    function compress($buffer) {
        //Remove CSS comments
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
        //Remove tabs, spaces, newlines, etc.
        $buffer = str_replace(array("
    ", "", "
    ", "\t", '  ', '    ', '    '), '', $buffer);
        return $buffer;
    }
    
    //Main style
    require_once($_SERVER['DOCUMENT_ROOT']."/templates/style/style.css");
    
    //Other style
    if($php_variable) {
        require_once($_SERVER['DOCUMENT_ROOT']."/templates/style/other.css");
    }
    
    ob_end_flush();
    ob_end_flush();
    ?>
    

    There's a lot of things going on here, so let me explain.

    Headers

    • Setting the file type as being CSS.
    • Setting the Last-Modified and Expires header for cache control (Setting at slightly over a week, you can change this).

    Minify and GZIP

    • Using ob_start, we tell it to GZIP this file as well as run a custom function compress.
    • compress removes all CSS comments and white space when sending to to the browser. This means you can keep all your comments and spacing the way you like it in your source files for easier editing, but only send the bare minimum to the browser.

    Style

    • Using require to import the stylesheets. Do this for as many stylesheets as you'd like; They'll all get delivered in a single HTTP request to the user.

    Using Your New File

    You'll call on the file just as you would a normal CSS file.

    <style type="text/css" src="/path/to/css-script.php"></style>
    

    Conclusion

    Using this method, you're doing several awesome things.

    • Provides the correct headers for cache'ing, making return visits to your site quick.
    • Minifys all the contents to contain no extra white space or comments.
    • Compresses the file using GZIP for smaller file size.
    • Includes all your stylesheets in one HTTP request, great for optimization.
    • Avoids using @import statements...which in itself is awesome.
    • Allows you to keep multiple stylesheets and use PHP logic to include or not include them.
    • Allows you to keep your source stylesheets spaced and commented as you like with no consequence to the visitors.

    You can use this same method for JavaScript as well, though the compress function above is strictly for CSS so I would omit it.

    Use this technique in combination with this cache control technique, and you've built yourself an awesome CSS/JS handler: How to force browser to reload cached CSS/JS files?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?