Did you find something wrong? Be sure to let us know with an issue. Thank you!
Minify
Webisters Minify shrinks HTML, CSS, and JavaScript before it leaves your server. It's stream-friendly, dependency-free, and safe with inline scripts and styles.
- Installation
- Getting Started
- Minifying HTML
- Minifying CSS
- Minifying JavaScript
- As response middleware
- Conclusion
Installation
composer require webisters/minify
Getting Started
Instantiate the Minify class and pass content to one of its methods:
use Framework\Minify\Minify;
$minify = new Minify();
Minifying HTML
$html = '<html>
<body>
<h1>Hello</h1>
</body>
</html>';
echo $minify->html($html);
// => <html><body><h1>Hello</h1></body></html>
Inline <script> and <style> tags are minified in-place using the matching CSS and JS minifiers.
Minifying CSS
$css = '
body {
background: #fff;
color: #0b1b2b;
}
';
echo $minify->css($css);
// => body{background:#fff;color:#0b1b2b}
Minifying JavaScript
$js = '
function greet(name) {
return "Hello, " + name + "!";
}
';
echo $minify->js($js);
// => function greet(name){return"Hello, "+name+"!";}
As response middleware
Hook Minify into your response pipeline so every HTML response gets shrunk automatically:
use Framework\Minify\Minify;
use Framework\HTTP\Response;
$response = new Response();
$response->setBody($minify->html($html));
$response->setHeader('Content-Length', (string) strlen($response->getBody()));
$response->send();
Pair this with gzip/brotli at the web-server level for the best wire weight.
Conclusion
Minification is a small win — usually 10-30% off your transfer size — but it's free, deterministic, and stacks well with compression.