Home

Awesome

Brotli Extension for PHP

Linux Windows

This extension allows Brotli compression.

Documentation for Brotli can be found at » https://github.com/google/brotli/.

Build

% 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

Distribution binary packages

Fedora / CentOS / RHEL

RPM packages of this extension are available in » Remi's RPM repository and are named php-brotli.

Configuration

php.ini:

extension=brotli.so

Output handler option

NameDefaultChangeable
brotli.output_compression0PHP_INI_ALL
brotli.output_compression_level-1PHP_INI_ALL

Constant

NameDescription
BROTLI_COMPRESS_LEVEL_MINMinimal compress level value
BROTLI_COMPRESS_LEVEL_MAXMaximal compress level value
BROTLI_COMPRESS_LEVEL_DEFAULTDefault compress level value

Function


brotli_compress — Compress a string

Description

string brotli_compress ( string $data [, int $quality = BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = BROTLI_GENERIC ] )

This function compress a string.

Parameters

Return Values

The compressed string or FALSE if an error occurred.


brotli_uncompress — Uncompress a compressed string

Description

string brotli_uncompress ( string $data [, int $length = 0 ] )

This function uncompress a compressed string.

Parameters

Return Values

The original uncompressed data or FALSE on error.


brotli_compress_init — Initialize an incremental compress context

Description

resource brotli_compress_init ( [ int $quality = BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = BROTLI_GENERIC ] )

Initialize an incremental compress context.

Parameters

Return Values

Returns a brotli context resource (brotli.state) on success, or FALSE on failure.


brotli_compress_add — Incrementally compress data

Description

string brotli_compress_add ( resource $context, string $data [, $mode = BROTLI_FLUSH ] )

Incrementally compress data.

Parameters

Return Values

Returns a chunk of compressed data, or FALSE on failure.


brotli_uncompress_init — Initialize an incremental uncompress context

Description

resource brotli_uncompress_init ( void )

Initialize an incremental uncompress context.

Return Values

Returns a brotli context resource (brotli.state) on success, or FALSE on failure.


brotli_uncompress_add — Incrementally uncompress data

Description

string brotli_uncompress_add ( resource $context, string $data [, $mode = BROTLI_FLUSH ] )

Incrementally uncompress data.

Parameters

Return Values

Returns a chunk of uncompressed data, or FALSE on failure.

Namespace

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\_FLUSH] )
function uncompress\_init()
function uncompress\_add( resource $context, string $data [, $mode = \\BROTLI\_FLUSH] )

alias functions..

Streams

Brotli compression and uncompression are available using the compress.brotli:// stream prefix.

Examples

$compressed = brotli_compress('Compresstest');

$uncompressed = brotli_uncompress($compressed);

echo $uncompressed;

Output handler

ini_set('brotli.output_compression', 'On');
// OR
// ob_start('ob_brotli_handler');

echo ...;

"Accept-Encoding: br" must be specified.

Namespace

$data = \Brotli\compress('test');

\Brotli\uncompress($data);

Streams

file_put_contents("compress.brotli:///patch/to/data.br", $data);
readfile("compress.brotli:///patch/to/data.br");

Incrementally

// compression
$resource = brotli_compress_init();
$compressed = '';
$compressed .= brotli_compress_add($resource, 'Hello, ', BROTLI_FLUSH);
$compressed .= brotli_compress_add($resource, 'World!', BROTLI_FLUSH);
$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_FLUSH);
$uncompressed .= brotli_uncompress_add($resource, substr($compressed, 5), BROTLI_FLUSH);
$uncompressed .= brotli_uncompress_add($resource, '', BROTLI_FINISH);

echo $uncompressed, PHP_EOL; // Hello, World!