PHP Класс HTTP_Encoder, minify

The "Vary: Accept-Encoding" header is sent. If the client allows encoding, Content-Encoding and Content-Length are added. Send a CSS file, compressed if possible $he = new HTTP_Encoder(array( 'content' => file_get_contents($cssFile) ,'type' => 'text/css' )); $he->encode(); $he->sendAll(); Shortcut to encoding output header('Content-Type: text/css'); // needed if not HTML HTTP_Encoder::output($css); Just sniff for the accepted encoding $encoding = HTTP_Encoder::getAcceptedEncoding(); For more control over headers, use getHeaders() and getData() and send your own output. Note: If you don't need header mgmt, use PHP's native gzencode, gzdeflate, and gzcompress functions for gzip, deflate, and compress-encoding respectively.
Автор: Stephen Clay ([email protected])
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$compressionLevel integer This level is used if encode() is not given a $compressionLevel
$encodeToIe6 boolean If you have many IE6 users and the bandwidth savings is worth troubling some of them, set this to true. By default, encoding is only offered to IE7+. When this is true, getAcceptedEncoding() will return an encoding for IE6 if its user agent string contains "SV1". This has been documented in many places as "safe", but there seem to be remaining, intermittent encoding bugs in patched IE6 on the wild web.

Защищенные свойства (Protected)

Свойство Тип Описание
$_content
$_encodeMethod
$_headers
$_useMbStrlen

Открытые методы

Метод Описание
__construct ( array $spec ) Get an HTTP Encoder object
encode ( integer $compressionLevel = null ) : boolean Encode (compress) the content
getAcceptedEncoding ( boolean $allowCompress = true, boolean $allowDeflate = true ) : array Determine the client's best encoding method from the HTTP Accept-Encoding header.
getContent ( ) : string Get content in current form
getHeaders ( ) : array Get array of output headers to be sent
isBuggyIe ( ) : boolean Is the browser an IE version earlier than 6 SP2?
output ( string $content, integer $compressionLevel = null ) : boolean Encode and send appropriate headers and content
sendAll ( ) Send output headers and content
sendHeaders ( ) Send output headers

Описание методов

__construct() публичный Метод

Get an HTTP Encoder object
public __construct ( array $spec )
$spec array options 'content': (string required) content to be encoded 'type': (string) if set, the Content-Type header will have this value. 'method: (string) only set this if you are forcing a particular encoding method. If not set, the best method will be chosen by getAcceptedEncoding() The available methods are 'gzip', 'deflate', 'compress', and '' (no encoding)

encode() публичный Метод

If the encode method is '' (none) or compression level is 0, or the 'zlib' extension isn't loaded, we return false. Then the appropriate gz_* function is called to compress the content. If this fails, false is returned. The header "Vary: Accept-Encoding" is added. If encoding is successful, the Content-Length header is updated, and Content-Encoding is also added.
public encode ( integer $compressionLevel = null ) : boolean
$compressionLevel integer given to zlib functions. If not given, the class default will be used.
Результат boolean success true if the content was actually compressed

getAcceptedEncoding() публичный статический Метод

If no Accept-Encoding header is set, or the browser is IE before v6 SP2, this will return ('', ''), the "identity" encoding. A syntax-aware scan is done of the Accept-Encoding, so the method must be non 0. The methods are favored in order of gzip, deflate, then compress. Deflate is always smallest and generally faster, but is rarely sent by servers, so client support could be buggier.
public static getAcceptedEncoding ( boolean $allowCompress = true, boolean $allowDeflate = true ) : array
$allowCompress boolean allow the older compress encoding
$allowDeflate boolean allow the more recent deflate encoding
Результат array two values, 1st is the actual encoding method, 2nd is the alias of that method to use in the Content-Encoding header (some browsers call gzip "x-gzip" etc.)

getContent() публичный Метод

Call after encode() for encoded content.
public getContent ( ) : string
Результат string

getHeaders() публичный Метод

E.g. array( 'Content-Length' => '615' ,'Content-Encoding' => 'x-gzip' ,'Vary' => 'Accept-Encoding' )
public getHeaders ( ) : array
Результат array

isBuggyIe() публичный статический Метод

Is the browser an IE version earlier than 6 SP2?
public static isBuggyIe ( ) : boolean
Результат boolean

output() публичный статический Метод

This is a convenience method for common use of the class
public static output ( string $content, integer $compressionLevel = null ) : boolean
$content string
$compressionLevel integer given to zlib functions. If not given, the class default will be used.
Результат boolean success true if the content was actually compressed

sendAll() публичный Метод

A shortcut for sendHeaders() and echo getContent() You must call this before headers are sent and it probably cannot be used in conjunction with zlib output buffering / mod_gzip. Errors are not handled purposefully.
public sendAll ( )

sendHeaders() публичный Метод

You must call this before headers are sent and it probably cannot be used in conjunction with zlib output buffering / mod_gzip. Errors are not handled purposefully.
См. также: getHeaders()
public sendHeaders ( )

Описание свойств

$_content защищенное свойство

protected $_content

$_encodeMethod защищенное свойство

protected $_encodeMethod

$_headers защищенное свойство

protected $_headers

$_useMbStrlen защищенное свойство

protected $_useMbStrlen

$compressionLevel публичное статическое свойство

This level is used if encode() is not given a $compressionLevel
public static int $compressionLevel
Результат integer

$encodeToIe6 публичное статическое свойство

If you have many IE6 users and the bandwidth savings is worth troubling some of them, set this to true. By default, encoding is only offered to IE7+. When this is true, getAcceptedEncoding() will return an encoding for IE6 if its user agent string contains "SV1". This has been documented in many places as "safe", but there seem to be remaining, intermittent encoding bugs in patched IE6 on the wild web.
public static bool $encodeToIe6
Результат boolean