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

Started on the old forum in July 2015 · view the original in the archive · old links redirect here

1 reply · 172 views · started 11 years ago · 1 min read

2 participants
GumSlone Administrator Principal · 1,690 posts #1

Speed Up your sites with the script below.

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

replace

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
$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/1024, 2)<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;

?>

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close