PHPackages                             kjdev/brotli - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. kjdev/brotli

ActivePhp-ext[Utility &amp; Helpers](/categories/utility)

kjdev/brotli
============

A compression/decompression with Brotli

0.18.3(5mo ago)1951.1k↑216.7%28[12 issues](https://github.com/kjdev/php-ext-brotli/issues)MITCPHP &gt;= 7.0.0CI passing

Since Apr 10Pushed 5mo ago9 watchersCompare

[ Source](https://github.com/kjdev/php-ext-brotli)[ Packagist](https://packagist.org/packages/kjdev/brotli)[ RSS](/packages/kjdev-brotli/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)DependenciesVersions (8)Used By (0)

Brotli Extension for PHP
========================

[](#brotli-extension-for-php)

[![Linux](https://github.com/kjdev/php-ext-brotli/actions/workflows/linux.yaml/badge.svg?branch=master)](https://github.com/kjdev/php-ext-brotli/actions/workflows/linux.yaml)[![Windows](https://github.com/kjdev/php-ext-brotli/actions/workflows/windows.yaml/badge.svg?branch=master)](https://github.com/kjdev/php-ext-brotli/actions/workflows/windows.yaml)

This extension allows Brotli compression.

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

Build
-----

[](#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
----------------------------

[](#distribution-binary-packages)

### Fedora / CentOS / RHEL

[](#fedora--centos--rhel)

RPM packages of this extension are available in [» Remi's RPM repository](https://rpms.remirepo.net/) and are named **php-brotli**.

Configuration
-------------

[](#configuration)

php.ini:

```
extension=brotli.so

```

### Output handler option

[](#output-handler-option)

NameDefaultChangeablebrotli.output\_compression0PHP\_INI\_ALLbrotli.output\_compression\_level11PHP\_INI\_ALLbrotli.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\_dict *string*

    Specifies the path to the compressed dictionary file to be used by the output handler.

    > can be used when `BROTLI_DICTIONARY_SUPPORT` is enabled

Constant
--------

[](#constant)

NameDescriptionBROTLI\_GENERICGeneric compress mode valueBROTLI\_TEXTText compress mode valueBROTLI\_FONTFont compress mode valueBROTLI\_COMPRESS\_LEVEL\_MINMinimal compress level valueBROTLI\_COMPRESS\_LEVEL\_MAXMaximal compress level valueBROTLI\_COMPRESS\_LEVEL\_DEFAULTDefault compress level valueBROTLI\_PROCESSIncremental process mode valueBROTLI\_FLUSHIncremental produce mode valueBROTLI\_FINISHIncremental finalize mode valueBROTLI\_DICTIONARY\_SUPPORTDictionary support value> `BROTLI_DICTIONARY_SUPPORT` must be enabled with brotli library version 1.1.0 or higher
>
> dictionary only work from compression level 5 or higher [note](google/brotli#1148)

Function
--------

[](#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

[](#brotli_compress--compress-a-string)

#### Description

[](#description)

```
brotli_compress ( string $data, int $level = BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = BROTLI_GENERIC, string|null $dict = null ): string|false
```

This function compress a string.

#### Parameters

[](#parameters)

- *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_SUPPORT` is enabled

#### Return Values

[](#return-values)

The compressed string or FALSE if an error occurred.

---

### brotli\_uncompress — Uncompress a compressed string

[](#brotli_uncompress--uncompress-a-compressed-string)

#### Description

[](#description-1)

```
brotli_uncompress ( string $data, string|null $dict = null ): string|false
```

This function uncompress a compressed string.

#### Parameters

[](#parameters-1)

- *data*

    The data compressed by brotli\_compress().
- *dict*

    The dictionary data.

    > can be used when `BROTLI_DICTIONARY_SUPPORT` is enabled

#### Return Values

[](#return-values-1)

The original uncompressed data or FALSE on error.

---

### brotli\_compress\_init — Initialize an incremental compress context

[](#brotli_compress_init--initialize-an-incremental-compress-context)

#### Description

[](#description-2)

```
brotli_compress_init ( int $level = BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = BROTLI_GENERIC, string|null $dict = null ): Brotli\Compress\Context|false
```

Initialize an incremental compress context.

#### Parameters

[](#parameters-2)

- *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_SUPPORT` is enabled

#### Return Values

[](#return-values-2)

Returns a `Brotli\Compress\Context` instance on success, or FALSE on failure.

---

### brotli\_compress\_add — Incrementally compress data

[](#brotli_compress_add--incrementally-compress-data)

#### Description

[](#description-3)

```
brotli_compress_add ( Brotli\Compress\Context $context, string $data, $mode = BROTLI\_FLUSH ): string|false
```

Incrementally compress data.

#### Parameters

[](#parameters-3)

- *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

[](#return-values-3)

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

---

### brotli\_uncompress\_init — Initialize an incremental uncompress context

[](#brotli_uncompress_init--initialize-an-incremental-uncompress-context)

#### Description

[](#description-4)

```
brotli_uncompress_init ( string|null $dict = null ): Brotli\UnCompress\Context|false
```

Initialize an incremental uncompress context.

#### Parameters

[](#parameters-4)

- *dict*

    The dictionary data.

    > can be used when `BROTLI_DICTIONARY_SUPPORT` is enabled

#### Return Values

[](#return-values-4)

Returns a `Brotli\UnCompress\Context` instance on success, or FALSE on failure.

---

### brotli\_uncompress\_add — Incrementally uncompress data

[](#brotli_uncompress_add--incrementally-uncompress-data)

#### Description

[](#description-5)

```
brotli_uncompress_add ( Brotli\UnCompress\Context $context, string $data, $mode = BROTLI\_FLUSH ): string|false
```

Incrementally uncompress data.

#### Parameters

[](#parameters-5)

- *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

[](#return-values-5)

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

Namespace
---------

[](#namespace)

```
Namespace Brotli;

function compress( string $data, int $level = \BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = \BROTLI_GENERIC, string|null $dict = null ): string|false {}
function uncompress( string $data, string|null $dict = null ): string|false {}
function compress_init( int $level = \BROTLI_COMPRESS_LEVEL_DEFAULT, int $mode = \BROTLI_GENERIC, string|null $dict = null ): \Brotli\Compress\Context|false {}
function compress_add( \Brotli\Compress\Context $context, string $data, $mode = \BROTLI_FLUSH ): string|false {}
function uncompress_init(string|null $dict = null): \Brotli\UnCompress\Context|false {}
function uncompress_add( \Brotli\UnCompress\Context $context, string $data, int $mode = \BROTLI_FLUSH ): string|false {}
```

alias functions..

Streams
-------

[](#streams)

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

Examples
--------

[](#examples)

```
$compressed = brotli_compress('Compresstest');

$uncompressed = brotli_uncompress($compressed);

echo $uncompressed;
```

### Output handler

[](#output-handler)

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

echo ...;
```

> "Accept-Encoding: br" must be specified.

### Namespace

[](#namespace-1)

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

\Brotli\uncompress($data);
```

### Streams

[](#streams-1)

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

### Incrementally

[](#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!
```

### Dictionary

[](#dictionary)

```
$data = '..';

// load dictionary data
$dict = file_get_contents('data.dict');

// basic
$compressed = brotli_compress(data: $data, dict: $dict);
$uncompressed = brotli_uncompress(data: $compressed, dict: $dict);

// incrementally
$context = brotli_compress_init(dict: $dict);
$compressed = '';
$compressed .= brotli_compress_add($context, $data);
$compressed .= brotli_compress_add($context, '', BROTLI_FINISH);

$context = brotli_uncompress_init(dict: $dict);
$uncompressed = '';
$uncompressed .= brotli_uncompress_add($context, $compressed, BROTLI_FLUSH);
$uncompressed .= brotli_uncompress_add($context, '', BROTLI_FINISH);

// streams
$ctx = stream_context_create([
    'brotli' => [
        'dict' => $dict,
    ],
]);

file_put_contents('compress.brotli:///path/to/data.br', $data, 0, $ctx);

$uncompressed = file_get_contents('compress.brotli:///path/to/data.br', false, $ctx);

// output handler
ini_set('brotli.output_compression_dict', __DIR__ . '/data.dict');
ini_set('brotli.output_compression', 'On');
// OR: ob_start('ob_brotli_handler');
echo ...;
```

> Experimental: [Compression Dictionary Transport](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Compression_dictionary_transport) support
>
> must be specified headers.
>
> - `Accept-Encoding: dcb`
> - `Available-Dictionary: ::`

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance69

Regular maintenance activity

Popularity38

Limited adoption so far

Community25

Small or concentrated contributor base

Maturity27

Early-stage or recently created project

 Bus Factor1

Top contributor holds 83.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~39 days

Recently: every ~49 days

Total

7

Last Release

167d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/18072f3d3a57cc6f8e11a0f405648caf4927261c0fc122f9cfa0d1441e087fb7?d=identicon)[kjdev](/maintainers/kjdev)

---

Top Contributors

[![kjdev](https://avatars.githubusercontent.com/u/465132?v=4)](https://github.com/kjdev "kjdev (221 commits)")[![remicollet](https://avatars.githubusercontent.com/u/270445?v=4)](https://github.com/remicollet "remicollet (11 commits)")[![JakubOnderka](https://avatars.githubusercontent.com/u/163343?v=4)](https://github.com/JakubOnderka "JakubOnderka (10 commits)")[![jnoordsij](https://avatars.githubusercontent.com/u/45041769?v=4)](https://github.com/jnoordsij "jnoordsij (8 commits)")[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (4 commits)")[![andypost](https://avatars.githubusercontent.com/u/73713?v=4)](https://github.com/andypost "andypost (2 commits)")[![jbboehr](https://avatars.githubusercontent.com/u/225601?v=4)](https://github.com/jbboehr "jbboehr (1 commits)")[![harryqt](https://avatars.githubusercontent.com/u/18257605?v=4)](https://github.com/harryqt "harryqt (1 commits)")[![esterTion](https://avatars.githubusercontent.com/u/7265411?v=4)](https://github.com/esterTion "esterTion (1 commits)")[![michael-grunder](https://avatars.githubusercontent.com/u/468149?v=4)](https://github.com/michael-grunder "michael-grunder (1 commits)")[![mjanda](https://avatars.githubusercontent.com/u/652954?v=4)](https://github.com/mjanda "mjanda (1 commits)")[![TBK](https://avatars.githubusercontent.com/u/858296?v=4)](https://github.com/TBK "TBK (1 commits)")[![jbanety](https://avatars.githubusercontent.com/u/1055330?v=4)](https://github.com/jbanety "jbanety (1 commits)")[![AlexeyZamorov](https://avatars.githubusercontent.com/u/53096396?v=4)](https://github.com/AlexeyZamorov "AlexeyZamorov (1 commits)")

### Embed Badge

![Health badge](/badges/kjdev-brotli/health.svg)

```
[![Health](https://phpackages.com/badges/kjdev-brotli/health.svg)](https://phpackages.com/packages/kjdev-brotli)
```

###  Alternatives

[zservices/query

Pacote para consultas em serviços do governo.

131.1k](/packages/zservices-query)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
