Download the PHP package akankov/html-min without Composer
On this page you can find all versions of the php package akankov/html-min. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download akankov/html-min
More information about akankov/html-min
Files in akankov/html-min
Package html-min
Short Description HTML compressor and minifier for PHP 8.3+
License MIT
Homepage https://github.com/akankov/html-min
Informations about the package html-min
html-min
A fast HTML5 compressor and minifier for PHP. Strips redundant whitespace, comments, optional tags, and default attributes, then sorts what's left so your gzip layer has less work to do.
Built on native \DOMDocument — no third-party DOM dependencies.
Requirements
- PHP 8.3, 8.4, or 8.5
- ext-dom, ext-libxml, ext-mbstring
Installation
Usage
Wrap any block in <nocompress>…</nocompress> to keep its whitespace intact.
Configuration
Every option is a chainable setter. All defaults are shown — the example below reproduces the default configuration.
Each setter returns $this, so you can configure and call minify() in one chain.
Inline CSS and JS minification
By default the contents of <style> and <script> blocks round-trip untouched.
Enable the two opt-in toggles to minify them:
The bundled minifiers are zero-dependency and conservative:
- CSS — strips
/* … */comments and collapses whitespace; the contents of strings andurl(…)are preserved. - JS — removes comments and collapses horizontal whitespace while preserving newlines (so Automatic Semicolon Insertion is unaffected), strings, regex literals, and template literals. Identifiers are never renamed.
Scripts that are not JavaScript are left alone automatically: a <script>
whose type is, for example, application/ld+json or text/x-template, and any
<script src="…">, passes through unminified.
Using a different minifier
For aggressive minification (identifier renaming, dead-code removal), plug in a
third-party tool with setInlineCssMinifier() / setInlineJsMinifier(). Each
takes any callable(string): string; pass null to restore the bundled default.
If a bundled minifier throws, the original source is kept and a warning is sent
to the PSR-3 logger (when one is set via setLogger()), so a minifier bug can
never corrupt the page. User-supplied callables let their exceptions propagate.
Extending
To run your own pass over every element during minification, implement
Akankov\HtmlMin\Contract\DomObserver and register it:
Benchmarks
Measured against voku/html-min, wyrihaximus/html-compress, zaininnari/html-minifier, and abordage/html-min on a corpus of real-world HTML pages.
| adapter | median ms/op | geomean ms/op | parse failures | avg gzipped ratio |
|---|---|---|---|---|
| akankov/html-min | 1.9 | 1.9 | 0 / 15 | 90.7% |
| akankov/html-min (inline) | 2.1 | 2.5 | 0 / 15 | 87.6% |
| voku/html-min | 3.3 | 3.6 | 0 / 15 | 90.7% |
| wyrihaximus/html-compress | 6.0 | 7.5 | 0 / 15 | 87.0% |
| zaininnari/html-minifier | 9.5 | 8.3 | 0 / 15 | 94.8% |
| abordage/html-min † | 0.2 | 0.2 | 0 / 15 | 90.2% |
The table above is regenerated by make bench from the latest run.
See latest.md for the per-fixture detail (speed, peak memory,
gzipped compression ratio, methodology, and non-claims). Reproduce with
make bench-install && make bench (requires Docker).
Development
CI runs the full matrix (PHP 8.3 / 8.4 / 8.5) on every push and pull request.
License
MIT — see LICENSE.
Originally authored by Lars Moelleken; maintained in this fork by Alex Kankov.
All versions of html-min with dependencies
ext-dom Version *
ext-libxml Version *
ext-mbstring Version *
psr/http-factory Version ^1.0
psr/http-message Version ^1.1 || ^2.0
psr/http-server-middleware Version ^1.0
psr/log Version ^3.0