PHP script to join multiple css files into one file, compress with gzip, set expiration headers

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

  • script PHP script to join multiple css files into one file, compress with gzip, set expiration headers

    Speed Up your sites with the script below.

    Join multiple css files into one file, compress them with gzip and set expiration header

    replace
    PHP Code:
    include('./css/file1.css');
    include(
    './css/file2.css');
    include(
    './css/file3.css'); 
    with your css files,
    PS. if you use images like in your css files you might need to update the relative path of the images in your css to work properly.

    PHP Code:
    <?php
    $offset 
    60 60 24 30// Cache for a day
    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");
    function 
    compress($buffer) {
        
    /* remove comments */
        
    $buffer preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!'''$buffer);
        
    /* remove tabs, spaces, newlines, etc. */
        
    $buffer str_replace(array("\r\n""\r""\n""\t"'  ''    ''    '), ''$buffer);
        
    $buffer str_replace(array(";}""; }"), '}'$buffer);
        
    $buffer str_replace(array('} '' }'), '}'$buffer);
        
    $buffer str_replace(array('{ ',' {'), '{'$buffer);
        
    $buffer str_replace('( ''('$buffer);
        
    $buffer str_replace(' )'')'$buffer);
        
    $buffer str_replace(array(': ',' :'), ':'$buffer);
        
    $buffer str_replace(array('("','(\''), '('$buffer);
        
    $buffer str_replace(array('")','\')'), ')'$buffer);
        
    $buffer str_replace(array(', ',' ,'), ','$buffer);
        
    $buffer str_replace(array('; ',' ;'), ';'$buffer);
        
        
    //$buffer = preg_replace("/url\(("|'|)((.*\.(png|gif|jpg))("|'|))\)/Uie", "image_to_base64('$3','$4')", $buffer);
        
        
    return $buffer;
    }
    function 
    image_to_base64($image_url,$image_type)
    {
        if(
    file_exists($image_url))
        {
            
    $size filesize($image_url);
            if(
    round($size/10242)<6)
            {
                
    $image_type str_replace('jpg','jpeg',$image_type);
                return 
    'url(\'data:image/'.$image_type.';base64,'.base64_encode(file_get_contents($image_url)).'\')';
            }
            else
                return 
    'url(\''.$image_url.'\')';
        }
        else
            return 
    'url(\''.$image_url.'\')';
    }

    ob_start();
    include(
    './css/file1.css');
    include(
    './css/file2.css');
    include(
    './css/file3.css');
    $out ob_get_contents();
    ob_end_clean();
    $out compress($out);

    if(
    strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip"))
    {
        
    header("Content-Encoding: gzip");
        
    header("Vary: Accept-Encoding");
        
    ob_start('ob_gzhandler');
    }
    echo 
    $out;

    ?>
    Last edited by GumSlone; 04.07.15, 14:37.

  • #2
    thanks GumSlone . it is very interesting idea
    Last edited by karson; 05.07.15, 06:21.

    Comment

    Working...
    X