/php-ext-brotli

Brotli Extension for PHP

Primary LanguageCMIT LicenseMIT

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

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).

Constant

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

Function

  • brotli_compress — Compress a string
  • brotli_uncompress — Uncompress a compressed string
  • brotli_compress_init — Initialize an incremental compress context
  • brotli_compress_add — Incrementally compress data
  • brotli_uncompress_init — Initialize an incremental uncompress context
  • brotli_uncompress_add — Incrementally uncompress data

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

  • 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) or BROTLI_FONT (for WOFF 2.0).

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

  • data

    The data compressed by brotli_compress().

  • length

    The maximum length of data to decode.

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

  • 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) or BROTLI_FONT (for WOFF 2.0).

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

  • context

    A context created with brotli_compress_init().

  • data

    A chunk of data to compress.

  • mode

    One of BROTLI_FLUSH (default) and BROTLI_PROCESS, BROTLI_FINISH.

    BROTLI_FINISH to terminate with the last chunk of data.

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

  • context

    A context created with brotli_uncompress_init().

  • data

    A chunk of compressed data.

  • mode

    One of BROTLI_FLUSH (default) and BROTLI_PROCESS, BROTLI_FINISH.

    BROTLI_FINISH to terminate with the last chunk of data.

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!