PHPackages                             middlewares/encoder - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. middlewares/encoder

ActiveLibrary[HTTP &amp; Networking](/categories/http)

middlewares/encoder
===================

Middleware to encode the response body to gzip or deflate

v2.2.0(1y ago)5142.1k↑66.3%2[1 PRs](https://github.com/middlewares/encoder/pulls)2MITPHPPHP ^7.2 || ^8.0CI passing

Since Oct 11Pushed 1y ago1 watchersCompare

[ Source](https://github.com/middlewares/encoder)[ Packagist](https://packagist.org/packages/middlewares/encoder)[ Docs](https://github.com/middlewares/encoder)[ RSS](/packages/middlewares-encoder/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (15)Used By (2)

middlewares/encoder
===================

[](#middlewaresencoder)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9e556d5199f309a8432c67e04f93c83eeac7ee38dac4a3217c79d1bf1fea10da/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6964646c6577617265732f656e636f6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/encoder)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Testing](https://github.com/middlewares/encoder/workflows/testing/badge.svg)](https://github.com/middlewares/encoder/workflows/testing/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/3099c7de0f6387d5dfae8d329a7aa19957fc7bf73e33e92c87066b247afc22c5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6964646c6577617265732f656e636f6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/encoder)

Middleware to encode the response body to `gzip` or `deflate` if the `Accept-Encoding` header is present and adds the `Content-Encoding` header. This package is splitted into the following components:

- [GzipEncoder](#gzipencoder)
- [DeflateEncoder](#deflateencoder)

You can use the component `ContentEncoding` in the [middlewares/negotiation](https://github.com/middlewares/negotiation#contentencoding) to negotiate the encoding to use.

Requirements
------------

[](#requirements)

- PHP &gt;= 7.2
- A [PSR-7 http library](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)
- A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)

Installation
------------

[](#installation)

This package is installable and autoloadable via Composer as [middlewares/encoder](https://packagist.org/packages/middlewares/encoder).

```
composer require middlewares/encoder
```

GzipEncoder
-----------

[](#gzipencoder)

Compress the response body to GZIP format using [gzencode](http://php.net/manual/en/function.gzencode.php) and add the header `Content-Encoding: gzip`.

**Note:** The response body is encoded only if the header contains the value `gzip` in the header `Accept-Encoding`.

```
Dispatcher::run([
	new Middlewares\GzipEncoder(),
]);
```

Optionally, you can provide a `Psr\Http\Message\StreamFactoryInterface` that will be used to create the response body. If it's not defined, [Middleware\\Utils\\Factory](https://github.com/middlewares/utils#factory) will be used to detect it automatically.

```
$streamFactory = new MyOwnStreamFactory();

$encoder = new Middlewares\GzipEncoder($streamFactory);
```

DeflateEncoder
--------------

[](#deflateencoder)

Compress the response body to Deflate format using [gzdeflate](http://php.net/manual/en/function.gzdeflate.php) and add the header `Content-Encoding: deflate`.

**Note:** The response body is encoded only if the header contains the value `deflate` in the header `Accept-Encoding`.

```
Dispatcher::run([
	new Middlewares\DeflateEncoder(),
]);
```

Optionally, you can provide a `Psr\Http\Message\StreamFactoryInterface` that will be used to create the response body. If it's not defined, [Middleware\\Utils\\Factory](https://github.com/middlewares/utils#factory) will be used to detect it automatically.

```
$streamFactory = new MyOwnStreamFactory();

$encoder = new Middlewares\DeflateEncoder($streamFactory);
```

Common Options
--------------

[](#common-options)

### contentType

[](#contenttype)

This option allows the overring of the default patterns used to detect what resources are already compressed.

The default pattern detects the following mime types `text/*`, `application/json`, `image/svg+xml` and empty content types as compressible. If the pattern begins with a forward slash `/` it is tested as a regular expression, otherwise its is case-insensitive string comparison.

```
Dispatcher::run([
	(new Middlewares\DeflateEncoder())
            ->contentType(
                    '/^application\/pdf$/', // Regular Expression
                    'text/csv' // Text Pattern
            )
]);
```

---

Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance46

Moderate activity, may be stable

Popularity38

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 86.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 ~237 days

Recently: every ~605 days

Total

14

Last Release

421d ago

Major Versions

v0.5.0 → v1.0.02018-01-27

v1.1.0 → v2.0.02019-11-29

PHP version history (4 changes)v0.1.0PHP ^5.6 || ^7.0

v0.5.0PHP ^7.0

v2.0.0PHP ^7.2

v2.1.1PHP ^7.2 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/e99df56b617f4af8cf0556a51a0ca20c7420104920c57d7e9eab093f92bb744f?d=identicon)[oscarotero](/maintainers/oscarotero)

![](https://www.gravatar.com/avatar/42e0d72f42eb7d84f67e20d28606da42e5a3248ca908b1eadb4366aafeae2561?d=identicon)[filisko](/maintainers/filisko)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (52 commits)")[![cseufert](https://avatars.githubusercontent.com/u/1734519?v=4)](https://github.com/cseufert "cseufert (6 commits)")[![filisko](https://avatars.githubusercontent.com/u/8798694?v=4)](https://github.com/filisko "filisko (2 commits)")

---

Tags

compressiondeflategziphttpmiddlewarepsr-15httppsr-7middlewarecompressionencodingpsr-15gzipdeflate

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/middlewares-encoder/health.svg)

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

###  Alternatives

[middlewares/negotiation

Middleware to implement content negotiation

47442.1k11](/packages/middlewares-negotiation)[middlewares/request-handler

Middleware to execute request handlers

451.6M26](/packages/middlewares-request-handler)[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[middlewares/trailing-slash

Middleware to normalize the trailing slash of the uri path

32506.3k11](/packages/middlewares-trailing-slash)[middlewares/payload

Middleware to parse the body of the request with support for json, csv and url-encode

32466.8k17](/packages/middlewares-payload)[middlewares/http-authentication

Middleware to implement Basic and Digest Http authentication

35302.0k2](/packages/middlewares-http-authentication)

PHPackages © 2026

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