I wanted to thank all the posting in this forum, but wanted to add one more thing I did to GREATLY decrease the mootools size. From the original 74K, down to ~40K thanks to this forum, and then down to ~17K w/ this compression script.
Please if anyone has any changes, post them here.
Just go into your template index.php files and replace all references to mootools.js or any others .css with something like the following:
Code: Select all
<link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/system/gzipcache.php?files=mootools.js,othercode1.js,othercode2.js" type="text/javascript" />
Of course, save the following code in a file gzipcache.php along side every css and js file you want to compress.
Code: Select all
<?php
ob_start('ob_gzhandler');
$files = $_GET["files"];
$found = false;
$filearray = explode(",", $files);
foreach ($filearray as $f)
{
if (substr($f, -4) == ".css")
{
header('Content-Type: text/css; charset: UTF-8');
$found = true;
$ext = ".css";
$extlength = -4;
break;
}
else if (substr($f, -3) == ".js")
{
header('Content-Type: text/javascript; charset: UTF-8');
$found = true;
$ext = ".js";
$extlength = -3;
break;
}
}
if (!$found || !isset($ext) || !isset($extlength))
{
die("Invalid Call");
return;
}
header('Cache-Control: must-revalidate');
$expire_offset = 0; // set to a reaonable interval, say 3600 (1 hr)
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expire_offset) . ' GMT');
foreach ($filearray as $f)
{
if (substr($f, $extlength) == $ext)
{
if ($ext == ".css")
dump_css_cache($f);
else
echo file_get_contents($f);
}
}
//Now the functions
function css_compress($buffer) {
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);// remove comments
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' '), '', $buffer);// remove tabs, spaces, newlines, etc.
$buffer = str_replace('{ ', '{', $buffer);// remove unnecessary spaces.
$buffer = str_replace(' }', '}', $buffer);
$buffer = str_replace('; ', ';', $buffer);
$buffer = str_replace(', ', ',', $buffer);
$buffer = str_replace(' {', '{', $buffer);
$buffer = str_replace('} ', '}', $buffer);
$buffer = str_replace(': ', ':', $buffer);
$buffer = str_replace(' ,', ',', $buffer);
$buffer = str_replace(' ;', ';', $buffer);
return $buffer;
}
function dump_css_cache($filename) {
$cwd = getcwd() . DIRECTORY_SEPARATOR;
$stat = stat($filename);
$current_cache = $cwd . $filename . '.' . $stat['size'] . '-' . $stat['mtime'] . '.cache';
// the cache exists - just dump it
if (is_file($current_cache)) {
include($current_cache);
return;
}
// remove any old, lingering caches for this file
if ($dead_files = glob($cwd . $filename . '.*.cache', GLOB_NOESCAPE))
foreach ($dead_files as $dead_file)
unlink($dead_file);
if (!function_exists('file_put_contents')) {
function file_put_contents($filename, $contents) {
$handle = fopen($filename, 'w');
fwrite($handle, $contents);
fclose($handle);
}
}
$cache_contents = css_compress(file_get_contents($filename));
file_put_contents($current_cache, $cache_contents);
echo $cache_contents;
}
?>
This code will strip down all CSS files of unnecessary characters, and then cache it in a file to save from having to do it for every page load. It will also attach all the header info to send javascript and CSS files encrypted using gzip, which will cut text based files down to more than half their original size. In addition, this file will combine any javascript and css files you have, which can greatly reduce page load time b/c there is overhead latency for each file a browser has to retrieve, so its better to have 1 css file as opposed to 10 that all sum up to the same size.