This port is based on version 2.4.8 (Jun 12, 2013) of the YUI compressor.
Table of Contents
Need a GUI?
We've made an awesome web based GUI to use the compressor, it's in the gui
folder.
We built the GUI because many times we need to compress some CSS code quick and easily.
GUI features:
- Optional on-the-fly LESS compilation before compression with error reporting included. We use LESS to write CSS so we spent some time to add LESS compilation before compression.
- Absolute control of the library.
How to use the GUI:
- You need a server with PHP 4.3+ installed (preferrably PHP 5).
- Download the repository and upload it to your server.
- Open your favourite browser and enter the URL to the
/gui
folder.
I don't need a GUI
OK, here's an example that covers a tipical use scenario:
<?php
// Require the compressor
require 'cssmin.php';
// Extract the CSS code you want to compress from your CSS files
$input_css1 = file_get_contents('test1.css');
$input_css2 = file_get_contents('test2.css');
// Create a new CSSmin object.
// By default CSSmin will try to raise PHP settings.
// If you don't want CSSmin to raise the PHP settings pass FALSE to
// the constructor i.e. $compressor = new CSSmin(false);
$compressor = new CSSmin();
// Override any PHP configuration options before calling run() (optional)
$compressor->set_memory_limit('256M');
$compressor->set_max_execution_time(120);
// Compress the CSS code in 1 long line and store the result in a variable
$output_css1 = $compressor->run($input_css1);
// You can change any PHP configuration option between run() calls
// and those will be applied for that run
$compressor->set_pcre_backtrack_limit(3000000);
$compressor->set_pcre_recursion_limit(150000);
// Compress the CSS code splitting lines after a specific column (2000) and
// store the result in a variable
$output_css2 = $compressor->run($input_css2, 2000);
// Do whatever you need with the compressed CSS code
echo $output_css1 . $output_css2;
You can also use Composer to install and autoload the compressor library:
$ composer.phar require tubalmartin/cssmin
After which the library would be loaded with all the other Composer packages when you include Composer's autoloader file:
require './vendor/autoload.php';
- Only one
@charset
at-rule per file and pushed at the beginning of the file. YUI compressor does not remove @charset at-rules if single quotes are used@charset 'utf-8'
. - Safer/improved comment removal. YUI compressor would ruin part of the output if the
*
selector is used right after a comment:a{/* comment 1 */*width:auto;}/* comment 2 */* html .b{height:100px}
. See issues #2528130, #2528118 & this topic background: none;
is not compressed tobackground:0;
anymore. See issue #2528127.text-shadow: 0 0 0;
is not compressed totext-shadow:0;
anymore. See issue #2528142- Trailing
;
is not removed anymore if the last property is prefixed with a*
(lte IE7 hack). See issue #2528146 - Newlines before and/or after a preserved comment
/*!
are not removed (we leave just 1 newline). YUI removes all newlines making it really hard to spot an important comment. - Spaces surrounding the
+
operator incalc()
calculations are not removed. YUI removes them and that is wrong. - Fix for issue #2528093.
- Fixes for
!important
related issues. - Fixes @keyframes 0% step bug.
- Some units should not be removed when the value is 0, such as
0s
or0ms
. - Fixes replacing 0 length values in selectors such as
.span0px { ... }
.
- Numbers & units compression:
- Sign is removed from positive numbers:
+2em
gets minified to2em
. - Leading and trailing zeros are removed:
0.2em
gets minified to.2em
,-01.010%
to-1.01%
,-9.0
to-9
. - Zero length numbers & units are replaced with
0
:-0.00%
,.0em
,0.0000
,-0px
get minified to0
. - Added newer unit lengths
ch, rem, vw, vh, vm, vmin
so we can replace0rem
or0vw
with0
.
- Sign is removed from positive numbers:
- Colors compression:
- Percentage and negative RGB values are supported i.e.
rgb(100%, 0%, 0%)
gets minified tored
. - HSL colors are compressed too, i.e.
hsl(0, 100%, 50%)
gets minified tored
. HSL angles are wrapped and values are clipped if needed.
- Percentage and negative RGB values are supported i.e.
- All CSS properties are lowercased.
Unit tests from YUI are not modified to fit this port so that you if a YUI test fails you can see the improvements/fixes this port provides over the original.
60+ unit tests written!!
How to run the test suite:
- You need a server with PHP 4.3+ installed (preferrably PHP 5).
- Download the repository and upload it to a folder in your server.
- Open your favourite browser and enter the URL to the file
tests/run.php
.
Description
Class constructor, creates a new CSSmin object.
Parameters
raise_php_limits
If TRUE, CSSmin will try to raise the values of some php configuration options. Set to FALSE to keep the values of your php configuration options. Defaults to TRUE.
Description
Minifies a string of uncompressed CSS code.
run()
may be called multiple times on a single CSSmin instance.
Parameters
css
A string of uncompressed CSS code.
Defaults to an empty string ''
.
linebreak_pos
Some source control tools don't like it when files containing lines longer than, say 8000 characters, are checked in. The linebreak option is used in that case to split long lines after a specific column. Defaults to FALSE (1 long line).
Return Values
A string of compressed CSS code or an empty string if no string is passed.
Description
Sets the memory_limit
configuration option for this script
CSSmin default value: 128M
Parameters
limit
Values & notes: memory_limit documentation
Description
Sets the max_execution_time
configuration option for this script
CSSmin default value: 60
Parameters
seconds
Values & notes: max_execution_time documentation
Description
Sets the pcre.backtrack_limit
configuration option for this script
CSSmin default value: 1000000
Parameters
limit
Values & notes: pcre.backtrack_limit documentation
Description
Sets the pcre.recursion_limit
configuration option for this script.
CSSmin default value: 500000
Parameters
limit
Values & notes: pcre.recursion_limit documentation
- Minify Minify is an HTTP content server. It compresses sources of content (usually files), combines the result and serves it with appropriate HTTP headers.
- Autoptimize is a Wordpress plugin. Autoptimize speeds up your website and helps you save bandwidth by aggregating and minimizing JS, CSS and HTML.
- IMPRESSPAGES PHP framework with content editor.
- Fixed all reported bugs: See issues #11, #13 (first case only) and #14.
- LESS compiler upgraded to version 1.7.0
- Chunk length reduced to 5000 chars (previously 25.000 chars) in an effort to avoid PCRE backtrack limits (needs feedback).
- Improvements for the @keyframes 0% step bug. Tests improved.
- Fix IE7 issue on matrix filters which browser accept whitespaces between Matrix parameters
- LESS compiler upgraded to version 1.4.2
- Fix for the @keyframes 0% step bug. Tests added.
- LESS compiler upgraded to version 1.4.1