Download the PHP package lastzero/css-optimizer without Composer
On this page you can find all versions of the php package lastzero/css-optimizer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lastzero/css-optimizer
More information about lastzero/css-optimizer
Files in lastzero/css-optimizer
Package css-optimizer
Short Description CSS Optimizer - Minifies stylesheets by removing duplicates and combining selectors
License MIT
Homepage https://github.com/lastzero/css-optimizer
Informations about the package css-optimizer
CSS Optimizer for PHP
Minifies stylesheets by removing duplicates and combining selectors - mostly safe and very efficient. This package was developed as a "quick win" to optimize the CSS of an existing commercial Web site, as the CSS contained many duplicates and similar selectors created by a CSS precompiler. It was published in the hope to help others in the same situation.
Note: The optimization is performed using simple (and fast) string functions - it's not a real CSS parser. It requires well formed CSS without inline comments (don't worry, you can use the minifyCss() method for that, see usage).
Warning: The order of selectors in your CSS does play a role and the "further down" one does in fact win when the specificity values are exactly the same. If your CSS relies on order to work, this optimization will obviously break it. See also https://en.wikipedia.org/wiki/Cascading_Style_Sheets#Specificity
Input:
body { foo:bar; too: me; }
body { baz:foo; }
body { foo: new }
div.test { border: 1px solid black; }
div.other { border: 1px solid black}
Output:
body{baz:foo;foo:new;too:me}div.other,div.test{border:1px solid black}
Usage:
$optimizer = new \CssOptimizer\Css\Optimizer;
$inputCss = 'body { foo:bar; too: me;} body { baz:foo; } body { foo: new }
div.test { border: 1px solid black; }
div.other { border: 1px solid black} ';
$minifiedCss = $optimizer->minifyCss($inputCss);
$optimizedCss = $optimizer->optimizeCss($minifiedCss);
print_r($optimizer->getCounts());
Assetic filter
This library comes with a ready-to-use Assetic filter (for Symfony2). Please use a pre-filter like CssMinFilter.
Usage:
$filter = new \CssOptimizer\Assetic\Filter\CssOptimizeFilter;
$asset = new StringAsset($inputCss);
$asset->ensureFilter($filter);
$outputCss = $asset->dump();
Composer
If you are using composer, simply add "lastzero/css-optimizer" to your composer.json file and run composer update
:
"require": {
"lastzero/css-optimizer": "*"
}