function Gzip($debug=0, $level=9) { function GzipEnc() { if (headers_sent() || connection_aborted()) { return 0; } if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false) return "x-gzip"; if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) return "gzip"; return 0; } header("Last-Modified: " . date('r', time()-60*60*10) ." GMT"); if (config('gzip') != 1) { if ($debug) echo $s; ob_end_flush(); return; } $enc = GzipEnc(); if ($enc) { $s = "\n<!-- Для вывода использовалось сжатие $enc -->\n"; $Contents = ob_get_contents(); ob_end_clean(); if ($debug) { $s .= "<!-- Общий размер файла: ".strlen($Contents)." байт "; $s .= "После сжатия: ".strlen(gzcompress($Contents, $level))." байт -->"; $Contents .= $s; } header("Content-Encoding: $enc"); print "\x1f\x8b\x08\x00\x00\x00\x00\x00"; $Size = strlen($Contents); $Crc = crc32($Contents); $Contents = gzcompress($Contents, $level); $Contents = substr($Contents, 0, strlen($Contents) - 4); print $Contents; print pack('V', $Crc); print pack('V', $Size); die(); } else { ob_end_flush(); die(); } }
Gzip(true);