compress/optimize css, reduce apache requests

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    compress/optimize css, reduce apache requests

    here is a small piece of code which will make you site load faster if you use multiple css files on it.
    put it intoo the directory with your css files.

    link to it will look like this:
    PHP Code:
    <link rel="stylesheet" href="./styles/style.css.php" title="stylesheet" type="text/css" /> 
    
    i'm using this code at my socialengine site.

    PHP Code:
    <?php
    $offset = 60 * 60 * 24 * 30; // Cache for a month
    header("Content-type: text/css");
    header ("Cache-Control: max-age=" . $offset . ", must-revalidate");
    header ("Expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT");
    
      ob_start("compress");
      function compress($buffer) {
        /* remove comments */
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
        /* remove tabs, spaces, newlines, etc. */
        $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
        /* remove last semicolon */
        $buffer = str_replace(array(";}", "; }"), '}', $buffer);
        return $buffer;
      }
    
      /* your css files */
      include('styles_global.css');
      include('styles.css');
      include('styles_album.css');
      include('styles_music.css');
      include('styles_blog.css');
      include('styles_video.css');
      include('styles_group.css');
      include('styles_apps.css');
      include('styles_newsfeed_advanced.css');
      include('styles_360player.css');
      include('gum_styles.css');
      include('shadetabs.css');
        ob_end_flush();
    ?>
    Advertise your mobile site for FREE with AdTwirl

Working...
X