Public Member Functions | |
| gzip_encode ($level=3, $debug=false, $outputCompressedSizes=0) | |
| gzip_accepted () | |
| get_complevel () | |
| linux_loadavg () | |
| freebsd_loadavg () | |
Public Attributes | |
| $_version = 0.66 | |
| $level | |
| $encoding | |
| $crc | |
| $size | |
| $gzsize | |
|
||||||||||||||||
|
Definition at line 150 of file class.gzip_encode.php. References $crc, $encoding, $level, $size, get_complevel(), and gzip_accepted(). 00150 {
00151 if (!function_exists('gzcompress')) {
00152 trigger_error('gzcompress not found, ' .
00153 'zlib needs to be installed for gzip_encode',
00154 E_USER_WARNING);
00155 return;
00156 }
00157 if (!function_exists('crc32')) {
00158 trigger_error('crc32() not found, ' .
00159 'PHP >= 4.0.1 needed for gzip_encode', E_USER_WARNING);
00160 return;
00161 }
00162 if (headers_sent()) return;
00163 if (connection_status() !== 0) return;
00164 $encoding = $this->gzip_accepted();
00165 if (!$encoding) return;
00166 $this->encoding = $encoding;
00167
00168 if ($level === true) {
00169 $level = $this->get_complevel();
00170 }
00171 $this->level = $level;
00172
00173 $contents = ob_get_contents();
00174 if ($contents === false) return;
00175
00176 $gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00"; // gzip header
00177
00178 // By Kasper Skaarhoj, start
00179 if ($outputCompressedSizes) {
00180 $contents.=chr(10)."<!-- Compressed, level ".$level.", original size was ".strlen($contents)." bytes. New size is ".strlen(gzcompress($contents, $level))." bytes -->";
00181 $size = strlen($contents); // Must set again!
00182 }
00183 // By Kasper Skaarhoj, end
00184
00185 $size = strlen($contents);
00186 $crc = crc32($contents);
00187 $gzdata .= gzcompress($contents, $level);
00188 $gzdata = substr($gzdata, 0, strlen($gzdata) - 4); // fix crc bug
00189 $gzdata .= pack("V",$crc) . pack("V", $size);
00190
00191 $this->size = $size;
00192 $this->crc = $crc;
00193 $this->gzsize = strlen($gzdata);
00194
00195 if ($debug) {
00196 return;
00197 }
00198
00199 ob_end_clean();
00200 Header('Content-Encoding: ' . $encoding);
00201 Header('Content-Length: ' . strlen($gzdata));
00202 Header('X-Content-Encoded-By: class.gzip_encode '.$this->_version);
00203
00204 echo $gzdata;
00205 }
|
|
|
Definition at line 309 of file class.gzip_encode.php. Referenced by get_complevel(). 00309 {
00310 $buffer= `uptime`;
00311 ereg("averag(es|e): ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]*)", $buffer, $load);
00312
00313 return max((float)$load[2], (float)$load[3], (float)$load[4]);
00314 }
|
|
|
Definition at line 267 of file class.gzip_encode.php. References $level, freebsd_loadavg(), and linux_loadavg(). Referenced by gzip_encode(). 00267 {
00268 $uname = posix_uname();
00269 switch ($uname['sysname']) {
00270 case 'Linux':
00271 $cl = (1 - $this->linux_loadavg()) * 10;
00272 $level = (int)max(min(9, $cl), 0);
00273 break;
00274 case 'FreeBSD':
00275 $cl = (1 - $this->freebsd_loadavg()) * 10;
00276 $level = (int)max(min(9, $cl), 0);
00277 break;
00278 default:
00279 $level = 3;
00280 break;
00281 }
00282 return $level;
00283 }
|
|
|
Definition at line 222 of file class.gzip_encode.php. References $encoding. Referenced by gzip_encode(). 00222 {
00223 if (strpos(getenv("HTTP_ACCEPT_ENCODING"), 'gzip') === false) return false;
00224 if (strpos(getenv("HTTP_ACCEPT_ENCODING"), 'x-gzip') === false) {
00225 $encoding = 'gzip';
00226 } else {
00227 $encoding = 'x-gzip';
00228 }
00229
00230 // Test file type. I wish I could get HTTP response headers.
00231 $magic = substr(ob_get_contents(),0,4);
00232 if (substr($magic,0,2) === '^_') {
00233 // gzip data
00234 $encoding = false;
00235 } else if (substr($magic,0,3) === 'GIF') {
00236 // gif images
00237 $encoding = false;
00238 } else if (substr($magic,0,2) === "\xFF\xD8") {
00239 // jpeg images
00240 $encoding = false;
00241 } else if (substr($magic,0,4) === "\x89PNG") {
00242 // png images
00243 $encoding = false;
00244 } else if (substr($magic,0,3) === 'FWS') {
00245 // Don't gzip Shockwave Flash files. Flash on windows incorrectly
00246 // claims it accepts gzip'd content.
00247 $encoding = false;
00248 } else if (substr($magic,0,2) === 'PK') {
00249 // pk zip file
00250 $encoding = false;
00251 }
00252
00253 return $encoding;
00254 }
|
|
|
Definition at line 290 of file class.gzip_encode.php. Referenced by get_complevel(). 00290 {
00291 $buffer = "0 0 0";
00292 $f = fopen("/proc/loadavg","rb");
00293 if (!feof($f)) {
00294 $buffer = fgets($f, 1024);
00295 }
00296 fclose($f);
00297 $load = explode(" ",$buffer);
00298 return max((float)$load[0], (float)$load[1], (float)$load[2]);
00299 }
|
|
|
Definition at line 124 of file class.gzip_encode.php. |
|
|
Definition at line 128 of file class.gzip_encode.php. Referenced by gzip_encode(). |
|
|
Definition at line 127 of file class.gzip_encode.php. Referenced by gzip_accepted(), and gzip_encode(). |
|
|
Definition at line 130 of file class.gzip_encode.php. |
|
|
Definition at line 126 of file class.gzip_encode.php. Referenced by get_complevel(), and gzip_encode(). |
|
|
Definition at line 129 of file class.gzip_encode.php. Referenced by gzip_encode(). |
1.3.8-20040913