PHP Class 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.
Author: Stephen Clay ([email protected])
Mostrar archivo Open project: mrclay/minify Class Usage Examples

Public Properties

Property Type Description
$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 Properties

Property Type Description
$_content
$_encodeMethod
$_headers
$_useMbStrlen

Public Methods

Method Description
__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

Method Details

__construct() public method

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() public method

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.
return boolean success true if the content was actually compressed

getAcceptedEncoding() public static method

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
return 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() public method

Call after encode() for encoded content.
public getContent ( ) : string
return string

getHeaders() public method

E.g. array( 'Content-Length' => '615' ,'Content-Encoding' => 'x-gzip' ,'Vary' => 'Accept-Encoding' )
public getHeaders ( ) : array
return array

isBuggyIe() public static method

Is the browser an IE version earlier than 6 SP2?
public static isBuggyIe ( ) : boolean
return boolean

output() public static method

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.
return boolean success true if the content was actually compressed

sendAll() public method

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() public method

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.
See also: getHeaders()
public sendHeaders ( )

Property Details

$_content protected_oe property

protected $_content

$_encodeMethod protected_oe property

protected $_encodeMethod

$_headers protected_oe property

protected $_headers

$_useMbStrlen protected_oe property

protected $_useMbStrlen

$compressionLevel public_oe static_oe property

This level is used if encode() is not given a $compressionLevel
public static int $compressionLevel
return integer

$encodeToIe6 public_oe static_oe property

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
return boolean