Download the PHP package ctw/ctw-middleware-htmlminifier without Composer
On this page you can find all versions of the php package ctw/ctw-middleware-htmlminifier. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ctw/ctw-middleware-htmlminifier
More information about ctw/ctw-middleware-htmlminifier
Files in ctw/ctw-middleware-htmlminifier
Package ctw-middleware-htmlminifier
Short Description This PSR-15 middleware formats, fixes and beautifies the HTML in the Response body using a variety of adapters.
License BSD-3-Clause
Informations about the package ctw-middleware-htmlminifier
Package "ctw/ctw-middleware-htmlminifier"
PSR-15 middleware that minifies HTML responses using pluggable adapters, reducing bandwidth and improving page load times.
Introduction
Why This Library Exists
HTML generated by template engines often contains excessive whitespace, comments, and formatting that increases page size without benefiting end users. Minifying HTML at the response level provides several advantages:
- Reduced bandwidth: Smaller responses transfer faster, especially on mobile networks
- Lower hosting costs: Less data transferred means reduced bandwidth bills
- Improved performance: Smaller payloads parse faster in browsers
- Production optimization: Keep source templates readable while serving optimized output
This middleware intercepts HTML responses and minifies them transparently, with multiple adapter options to balance speed versus compression ratio.
Problems This Library Solves
- Bloated HTML: Template engines produce readable but unnecessarily large output
- Manual optimization: Without automation, developers must manually minify HTML
- Inconsistent minification: Different approaches across a codebase lead to varying output quality
- Build complexity: Template-level minification complicates the build process
- Debugging difficulty: Aggressive minification can break HTML; adapters provide different safety levels
Where to Use This Library
- Production environments: Reduce HTML size for faster page loads
- High-traffic sites: Minimize bandwidth costs at scale
- Mobile-first applications: Optimize for slower network connections
- Content-heavy pages: Blog posts, documentation, and marketing pages benefit most
- API responses: HTML fragments returned by AJAX endpoints
Design Goals
- Pluggable adapters: Choose the right minification strategy for your needs
- Safe defaults: Conservative minification that won't break valid HTML
- Transparent operation: Works automatically on HTML responses only
- Statistics tracking: Appends an HTML comment showing compression ratio
- Zero template changes: Minifies at response level, not template level
Requirements
- PHP 8.3 or higher
- ext-tidy
- ctw/ctw-middleware ^4.0
Installation
Install by adding the package as a Composer requirement:
Usage Examples
Basic Pipeline Registration (Mezzio)
ConfigProvider Registration
Available Adapters
| Adapter | Description | Speed | Compression |
|---|---|---|---|
SimpleAdapter |
Regex-based whitespace removal | Fast | Moderate |
TidyAdapter |
Uses PHP's Tidy extension for safe minification | Medium | Good |
WyriHaximusAdapter |
Full-featured HTML compressor (requires additional package) | Slower | Best |
Using SimpleAdapter
The SimpleAdapter uses regular expressions for fast, lightweight minification:
Performs:
- Removes multi-line whitespace
- Removes HTML comments
- Collapses multiple spaces
- Strips whitespace around tags
Using TidyAdapter
The TidyAdapter leverages PHP's Tidy extension for standards-compliant minification:
Features:
- Fixes malformed HTML
- Validates HTML5 doctype
- Configurable output formatting
- Safe for production use
Using WyriHaximusAdapter
For maximum compression, install the optional dependency:
Output Statistics
The middleware appends an HTML comment showing compression statistics:
| Field | Description |
|---|---|
in |
Original HTML size in bytes |
out |
Minified HTML size in bytes |
diff |
Percentage reduction achieved |
Selective Processing
The middleware automatically:
- Only processes responses with
Content-Type: text/htmlorapplication/xhtml - Passes through empty responses unchanged
- Skips non-HTML responses (JSON, images, etc.)
All versions of ctw-middleware-htmlminifier with dependencies
ext-tidy Version *
ctw/ctw-middleware Version ^5.0
psr/container Version ^1.0 || ^2.0


