PTTVDesign
Захожу иногда

Репутация: +1/-0
Offline
Сообщений: 11
interbuyer
|
 |
« : 09.12.2007, 11:48:19 » |
|
вот хотел предложить вывод CSS, JS посредством сжатия GZIP
template_css.php: <?php @error_reporting(0); require_once("/home/penzin/public_html/honda24/templates/honda24/zip.php");
initGzip_quik(); 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); $buffer = str_replace(';}', '}', $buffer); return $buffer; }
function dump_css_cache($filename) { $cwd = getcwd() . DIRECTORY_SEPARATOR;
$stat = stat($filename); $current_cache = $cwd . '_optimize_' . $filename;
// 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 . '_optimize_' . $filename, 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('template_css.css'); doGzip_quik(); ?>
zip.php: <? /** * Initialise GZIP */ function initGzip_quik() { global $mosConfig_gzip, $do_gzip_compress;
$do_gzip_compress = FALSE; $phpver = phpversion(); $useragent = $_SERVER['HTTP_USER_AGENT']; $canZip = $_SERVER['HTTP_ACCEPT_ENCODING'];
$gzip_check = 0; $zlib_check = 0; $gz_check = 0; $zlibO_check = 0; $sid_check = 0; if ( strpos( $canZip, 'gzip' ) !== false) { $gzip_check = 1; } if ( extension_loaded( 'zlib' ) ) { $zlib_check = 1; } if ( function_exists('ob_gzhandler') ) { $gz_check = 1; } if ( ini_get('zlib.output_compression') ) { $zlibO_check = 1; } if ( ini_get('session.use_trans_sid') ) { $sid_check = 1; }
if ( $phpver >= '4.0.4pl1' && ( strpos($useragent,'compatible') !== false || strpos($useragent,'Gecko') !== false ) ) { // Check for gzip header or northon internet securities or session.use_trans_sid if ( ( $gzip_check || isset( $_SERVER['---------------']) ) && $zlib_check && $gz_check && !$zlibO_check && !$sid_check ) { // You cannot specify additional output handlers if // zlib.output_compression is activated here ob_start( 'ob_gzhandler' ); return; } } else if ( $phpver > '4.0' ) { if ( $gzip_check ) { if ( $zlib_check ) { $do_gzip_compress = TRUE; ob_start(); ob_implicit_flush(0);
header( 'Content-Encoding: gzip' ); return; } } }
ob_start(); }
/** * Perform GZIP */ function doGzip_quik() { global $do_gzip_compress; if ( $do_gzip_compress ) { /** *Borrowed from php.net! */ $gzip_contents = ob_get_contents(); ob_end_clean();
$gzip_size = strlen($gzip_contents); $gzip_crc = crc32($gzip_contents);
$gzip_contents = gzcompress($gzip_contents, 9); $gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4);
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00"; echo $gzip_contents; echo pack('V', $gzip_crc); echo pack('V', $gzip_size); } else { ob_end_flush(); } } ?>
генерируется _optimize_template_css.css на тут случай если бруизер не поддерживает CSS как PHP (Firefox) можно сначала вызвать template_css.php а потом использовать _optimize_template_css.css
|