This extension allows Brotli compression.
Documentation for Brotli can be found at » https://github.com/google/brotli/.
% git clone --recursive --depth=1 https://github.com/kjdev/php-ext-brotli.git
% cd php-ext-brotli
% phpize
% ./configure
% make
$ make install
To use the system library (using pkg-config)
% ./configure --with-libbrotli
RPM packages of this extension are available in » Remi's RPM repository and are named php-brotli.
php.ini:
extension=brotli.so
Name | Default | Changeable |
---|---|---|
brotli.output_compression | 0 | PHP_INI_ALL |
brotli.output_compression_level | -1 | PHP_INI_ALL |
-
brotli.output_compression boolean
Whether to transparently compress pages. If this option is set to "On" in php.ini or the Apache configuration, pages are compressed if the browser sends an "Accept-Encoding: br" header. "Content-Encoding: br" and "Vary: Accept-Encoding" headers are added to the output. In runtime, it can be set only before sending any output.
-
brotli.output_compression_level integer
Compression level used for transparent output compression. Specify a value between 0 to 11. The default value of -1 uses internally defined values (11).
Available since PHP 5.4.0.
Name | Description |
---|---|
BROTLI_COMPRESS_LEVEL_MIN | Minimal compress level value |
BROTLI_COMPRESS_LEVEL_MAX | Maximal compress level value |
BROTLI_COMPRESS_LEVEL_DEFAULT | Default compress level value |
- brotli_compress — Compress a string
- brotli_uncompress — Uncompress a compressed string
- brotli_compress_init — Initialize an incremental compress context (PHP 7)
- brotli_compress_add — Incrementally compress data (PHP 7)
- brotli_uncompress_init — Initialize an incremental uncompress context (PHP 7)
- brotli_uncompress_add — Incrementally uncompress data (PHP 7)
string brotli_compress ( string $data [, int $quality = BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = BROTLI_GENERIC ] )
This function compress a string.
-
data
The data to compress.
-
quality
The higher the quality, the slower the compression. (Defaults to
BROTLI\_COMPRESS\_LEVEL\_DEFAULT
) -
mode
The compression mode can be
BROTLI_GENERIC
(default),BROTLI_TEXT
(for UTF-8 format text input) orBROTLI_FONT
(for WOFF 2.0).
The compressed string or FALSE if an error occurred.
string brotli_uncompress ( string $data [, int $length = 0 ] )
This function uncompress a compressed string.
-
data
The data compressed by brotli_compress().
-
length
The maximum length of data to decode.
The original uncompressed data or FALSE on error.
resource brotli_compress_init ( [ int $quality = BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = BROTLI_GENERIC ] )
Initialize an incremental compress context. (PHP 7)
-
quality
The higher the quality, the slower the compression. (Defaults to
BROTLI\_COMPRESS\_LEVEL\_DEFAULT
) -
mode
The compression mode can be
BROTLI_GENERIC
(default),BROTLI_TEXT
(for UTF-8 format text input) orBROTLI_FONT
(for WOFF 2.0).
Returns a brotli context resource (brotli.state) on success, or FALSE on failure.
string brotli_compress_add ( resource $context, string $data [, $mode = BROTLI_PROCESS ] )
Incrementally compress data. (PHP 7)
-
context
A context created with
brotli_compress_init()
. -
data
A chunk of data to compress.
-
mode
One of
BROTLI_PROCESS
(default),BROTLI_FINISH
.BROTLI_FINISH
to terminate with the last chunk of data.
Returns a chunk of compressed data, or FALSE on failure.
resource brotli_uncompress_init ( void )
Initialize an incremental uncompress context. (PHP 7)
Returns a brotli context resource (brotli.state) on success, or FALSE on failure.
string brotli_uncompress_add ( resource $context, string $data [, $mode = BROTLI_PROCESS ] )
Incrementally uncompress data. (PHP 7)
-
context
A context created with
brotli_uncompress_init()
. -
data
A chunk of compressed data.
-
mode
One of
BROTLI_PROCESS
(default),BROTLI_FINISH
.BROTLI_FINISH
to terminate with the last chunk of data.
Returns a chunk of uncompressed data, or FALSE on failure.
Namespace Brotli;
function compress( $data [, $quality = \\BROTLI\_COMPRESS\_LEVEL\_DEFAULT, $mode = \\BROTLI\_GENERIC ] )
function uncompress( $data [, $length = 0 ] )
function compress\_init( [ $quality = \\BROTLI\_COMPRESS\_LEVEL\_DEFAULT, $mode = \\BROTLI\_GENERIC ] )
function compress\_add( resource $context, string $data [, $mode = \\BROTLI\_PROCESS] )
function uncompress\_init()
function uncompress\_add( resource $context, string $data [, $mode = \\BROTLI\_PROCESS] )
alias functions..
Brotli compression and uncompression are available using the
compress.brotli://
stream prefix.
$compressed = brotli_compress('Compresstest');
$uncompressed = brotli_uncompress($compressed);
echo $uncompressed;
ini_set('brotli.output_compression', 'On');
// OR
// ob_start('ob_brotli_handler');
echo ...;
"Accept-Encoding: br" must be specified.
$data = \Brotli\compress('test');
\Brotli\uncompress($data);
file_put_contents("compress.brotli:///patch/to/data.br", $data);
readfile("compress.brotli:///patch/to/data.br");
// compression
$resource = brotli_compress_init();
$compressed = '';
$compressed .= brotli_compress_add($resource, 'Hello, ', BROTLI_PROCESS);
$compressed .= brotli_compress_add($resource, 'World!', BROTLI_PROCESS);
$compressed .= brotli_compress_add($resource, '', BROTLI_FINISH);
echo brotli_uncompress($compressed), PHP_EOL; // Hello, World!
// uncompression
$resource = brotli_uncompress_init();
$uncompressed = '';
$uncompressed .= brotli_uncompress_add($resource, substr($compressed, 0, 5), BROTLI_PROCESS);
$uncompressed .= brotli_uncompress_add($resource, substr($compressed, 5), BROTLI_PROCESS);
$uncompressed .= brotli_uncompress_add($resource, '', BROTLI_FINISH);
echo $uncompressed, PHP_EOL; // Hello, World!