note 71299 deleted from function.ob-start by cmb
| From: | cmb@php.net | Date: | Sun, 14 Jul 2019 11:26:53 +0000 |
| Subject: | note 71299 deleted from function.ob-start by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211575@lists.php.net to get a copy of this message | ||
Note Submitter: lucky760 at yahoo dot com
----
Just for simplicity's sake (and because I had to rewrite it for use at http://www.VideoSift.com anyway), here's a very simplified,
pre-PHP5 version. Just add one call to dump_css_cache() for each of your CSS files.
<?php
ob_start('ob_gzhandler');
header('Content-Type: text/css; charset: UTF-8');
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');
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;
}
dump_css_cache('_general.css');
?>