适用于 PHP 的 Brotli 扩展
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 installTo use the system library (using pkg-config)
% ./configure --with-libbrotliRPM 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 | 11 | PHP_INI_ALL |
| brotli.output_compression_exclude_types | "" | PHP_INI_ALL |
| brotli.output_compression_dict | "" | 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 BROTLI_COMPRESS_LEVEL_DEFAULT (11).
brotli.output_compression_exclude_types string
The extension contains a built-in list of non-compressible MIME types. The list can be found in the phpinfo() output. If something is missing, more MIME types can be added with this ini setting.
Comma-separated list of MIME types that should not be compressed by the transparent output handler.
Supports exact MIME type matches and wildcard family matches.
A wildcard entry must use the type/* form.
Example (both of these are already in the built-in list):
brotli.output_compression_exclude_types="video/*,application/pdf"This is useful for already-compressed binary formats where additional Brotli compression usually provides little benefit.
A token prefixed with ! negates a match: it makes the extension
compress that MIME type even if it is in the built-in exclusion list.
Negation also supports exact matches and type/* wildcards. Since
! is a special character in php.ini, values that use it must be
quoted.
brotli.output_compression_exclude_types="!image/png" Within the same list, if a MIME type matches multiple tokens, the
last matching token wins, regardless of whether it is positive or
negative (the same precedence rule .gitignore uses). This allows
patterns such as:
brotli.output_compression_exclude_types="image/*,!image/png" which excludes all image/* types except image/png, since the
negation is listed after the wildcard.
Negation only applies within this setting: if it doesn't produce a definitive match for the current MIME type, the built-in list is still consulted. The built-in list's contents can be seen in the phpinfo() output.
brotli.output_compression_dict string
Specifies the path to the compressed dictionary file to be used by the output handler.
can be used when
BROTLI_DICTIONARY_SUPPORTis enabled
| Name | Description |
|---|---|
| BROTLI_GENERIC | Generic compress mode value |
| BROTLI_TEXT | Text compress mode value |
| BROTLI_FONT | Font compress mode value |
| 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_PROCESS | Incremental process mode value |
| BROTLI_FLUSH | Incremental produce mode value |
| BROTLI_FINISH | Incremental finalize mode value |
| BROTLI_DICTIONARY_SUPPORT | Dictionary support value |
BROTLI_DICTIONARY_SUPPORTmust be enabled with brotli library version 1.1.0 or higherdictionary only work from compression level 5 or higher note
brotli_compress ( string $data, int $level = BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = BROTLI_GENERIC, string|null $dict = null ): string|falseThis function compress a string.
data
The data to compress.
level
The higher the level, 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).
dict
The dictionary data.
can be used when
BROTLI_DICTIONARY_SUPPORTis enabled
The compressed string or FALSE if an error occurred.
brotli_uncompress ( string $data, string|null $dict = null ): string|falseThis function uncompress a compressed string.
data
The data compressed by brotli_compress().
dict
The dictionary data.
can be used when
BROTLI_DICTIONARY_SUPPORTis enabled
The original uncompressed data or FALSE on error.
brotli_compress_init ( int $level = BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = BROTLI_GENERIC, string|null $dict = null ): Brotli\Compress\Context|falseInitialize an incremental compress context.
level
The higher the level, 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).
dict
The dictionary data.
can be used when
BROTLI_DICTIONARY_SUPPORTis enabled
Returns a Brotli\Compress\Context instance on success,
or FALSE on failure.
brotli_compress_add ( Brotli\Compress\Context $context, string $data, $mode = BROTLI\_FLUSH ): string|falseIncrementally compress data.
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.
Returns a chunk of compressed data, or FALSE on failure.
brotli_uncompress_init ( string|null $dict = null ): Brotli\UnCompress\Context|falseInitialize an incremental uncompress context.
dict
The dictionary data.
can be used when
BROTLI_DICTIONARY_SUPPORTis enabled
Returns a Brotli\UnCompress\Context instance on success,
or FALSE on failure.
brotli_uncompress_add ( Brotli\UnCompress\Context $context, string $data, $mode = BROTLI\_FLUSH ): string|falseIncrementally uncompress data.
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.
Returns a chunk of uncompressed data, or FALSE on failure.
…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:///path/to/data.br", $data);
readfile("compress.brotli:///path/to/data.br");……Experimental: Compression Dictionary Transport support
must be specified headers.
Accept-Encoding: dcbAvailable-Dictionary: ::
暂无开放 Issues,或尚未同步最近议题。