zipRatio zipDebug function GzDocOut zipRatio zipDebug if isset _SERVER

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 0 <= zipRatio <= 9
// 0 <= zipDebug <= 2
function GzDocOut($zipRatio = 3, $zipDebug = 2)
{
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
$AE = $_SERVER['HTTP_ACCEPT_ENCODING'];
else
$AE = $_SERVER['HTTP_TE'];
if (ob_get_length() == 0 || !isset($AE))
{
ob_end_flush();
exit;
}
$enc_ar = explode(',', $AE);
$mayZip = false;
$encoding = '';
foreach ($enc_ar as $enc)
{
$enc = trim($enc);
if ('gzip' === $enc || 'x-gzip' === $enc)
{
$mayZip = true;
$encoding = $enc;
break;
}
}
if (!$mayZip)
{
ob_end_flush();
exit;
}
$buf = ob_get_contents();
// HTML-минимизация
$buf = preg_replace('/(?![^<]*<\/pre>)[\n\r\t]+/', "\n", $buf);
$buf = preg_replace('/ {2,}/', ' ', $buf);
$buf = preg_replace('/>[\n]+/', '>', $buf);
ob_end_clean();
$bufZiped = gzcompress($buf, $zipRatio);
if ($zipDebug)
{
$bufLen = strlen($buf);
$bufZipedLen= strlen($bufZiped);
$buf .= (2 == $zipDebug) ? "\n\n<!-- " : '<br><center><font style="color:#C0C0C0; font-size:9px;">';
$buf .= 'Original length: '.$bufLen.'; ';
$buf .= 'Length with GZIP: '.$bufZipedLen.'; ';
$buf .= (2 == $zipDebug) ? " -->\n" : '</font></center>';
$bufZiped = gzcompress($buf, $zipRatio);
}
$bufZiped = pack('cccccccc',0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00)
.substr($bufZiped, 0, -4)
.pack('V',crc32($buf))
.pack('V',strlen($buf));
header('Content-encoding: '.$encoding);
header('Content-length: '.strlen($bufZiped));
echo $bufZiped;
exit;
}