How to compress CSS and JS files with PHP?
A simple step-by-step guide which will take less than 5 minutes
There are many minifiers to choose from. In this post I'll present you a minifier by Matthias Mullie, available under the terms of MIT license.Installation using composer
composer require matthiasmullie/minify
Installation from a ZIP
then you need to specify paths to files:
include 'Minify.php';
include 'CSS.php';
include 'JS.php';
include 'Exception.php';
include 'Exceptions/BasicException.php';
include 'Exceptions/FileImportException.php';
include 'Exceptions/IOException.php';
include 'ConverterInterface.php';
include 'Converter.php';
Minification of CSS files
use MatthiasMullie\Minify;
$minifier = new Minify\CSS($inputName);
$minifier->minify($outputName);
oruse MatthiasMullie\Minify;
$minifier = new Minify\CSS($cssCode);
$result = $minifier->minify();
Minification of Javascript files
use MatthiasMullie\Minify;
$minifier = new Minify\JS($inputName);
$minifier->minify($outputName);
oruse MatthiasMullie\Minify;
$minifier = new Minify\JS($jsCode);
$result = $minifier->minify();
Comments