{"id":13712024,"url":"https://github.com/middlewares/encoder","last_synced_at":"2026-01-07T23:54:54.376Z","repository":{"id":48721659,"uuid":"70580342","full_name":"middlewares/encoder","owner":"middlewares","description":"PSR-15 middleware to encode the response body to gzip or deflate","archived":false,"fork":false,"pushed_at":"2025-03-26T14:44:07.000Z","size":59,"stargazers_count":5,"open_issues_count":2,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-01T00:39:58.284Z","etag":null,"topics":["compression","deflate","gzip","http","middleware","psr-15"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/middlewares.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-11T10:01:38.000Z","updated_at":"2025-03-23T10:27:17.000Z","dependencies_parsed_at":"2024-06-19T17:37:34.486Z","dependency_job_id":"5b059b6b-6a95-41d8-89d1-6bd0f7b12551","html_url":"https://github.com/middlewares/encoder","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fencoder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fencoder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fencoder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fencoder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/middlewares","download_url":"https://codeload.github.com/middlewares/encoder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252772254,"owners_count":21801889,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compression","deflate","gzip","http","middleware","psr-15"],"created_at":"2024-08-02T23:01:14.158Z","updated_at":"2026-01-07T23:54:54.331Z","avatar_url":"https://github.com/middlewares.png","language":"PHP","readme":"# middlewares/encoder\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE)\n![Testing][ico-ga]\n[![Total Downloads][ico-downloads]][link-downloads]\n\nMiddleware 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:\n\n* [GzipEncoder](#gzipencoder)\n* [DeflateEncoder](#deflateencoder)\n\nYou can use the component `ContentEncoding` in the [middlewares/negotiation](https://github.com/middlewares/negotiation#contentencoding) to negotiate the encoding to use.\n\n## Requirements\n\n* PHP \u003e= 7.2\n* A [PSR-7 http library](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)\n* A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)\n\n## Installation\n\nThis package is installable and autoloadable via Composer as [middlewares/encoder](https://packagist.org/packages/middlewares/encoder).\n\n```sh\ncomposer require middlewares/encoder\n```\n\n## GzipEncoder\n\nCompress the response body to GZIP format using [gzencode](http://php.net/manual/en/function.gzencode.php) and add the header `Content-Encoding: gzip`.\n\n**Note:** The response body is encoded only if the header contains the value `gzip` in the header `Accept-Encoding`.\n\n```php\nDispatcher::run([\n\tnew Middlewares\\GzipEncoder(),\n]);\n```\n\nOptionally, 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.\n\n```php\n$streamFactory = new MyOwnStreamFactory();\n\n$encoder = new Middlewares\\GzipEncoder($streamFactory);\n```\n\n## DeflateEncoder\n\nCompress the response body to Deflate format using [gzdeflate](http://php.net/manual/en/function.gzdeflate.php) and add the header `Content-Encoding: deflate`.\n\n**Note:** The response body is encoded only if the header contains the value `deflate` in the header `Accept-Encoding`.\n\n```php\nDispatcher::run([\n\tnew Middlewares\\DeflateEncoder(),\n]);\n```\n\nOptionally, 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.\n\n```php\n$streamFactory = new MyOwnStreamFactory();\n\n$encoder = new Middlewares\\DeflateEncoder($streamFactory);\n```\n\n## Common Options\n\n### contentType\n\nThis option allows the overring of the default patterns used to detect what resources are already compressed.\n\nThe 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.\n\n```php\nDispatcher::run([\n\t(new Middlewares\\DeflateEncoder())\n            -\u003econtentType(\n                    '/^application\\/pdf$/', // Regular Expression\n                    'text/csv' // Text Pattern\n            )\n]);\n```\n---\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.\n\nThe MIT License (MIT). Please see [LICENSE](LICENSE) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/middlewares/encoder.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-ga]: https://github.com/middlewares/encoder/workflows/testing/badge.svg\n[ico-downloads]: https://img.shields.io/packagist/dt/middlewares/encoder.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/middlewares/encoder\n[link-downloads]: https://packagist.org/packages/middlewares/encoder\n","funding_links":[],"categories":["Packages"],"sub_categories":["Content"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiddlewares%2Fencoder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiddlewares%2Fencoder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiddlewares%2Fencoder/lists"}