A PHP port of the YUI CSS compressor.
CSS PHP JavaScript Shell


A PHP port of the YUI CSS compressor

This port is based on version 2.4.8 (Jun 12, 2013) of the YUI compressor.

Table of Contents

  1. How to use
  2. YUI compressor on asteroids!
    1. Bugs fixed
    2. Enhancements
  3. Unit Tests
  4. API Reference
  5. Who uses this?
  6. Changelog

1. How to use

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 5.0.0+ installed.
  • 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:


// 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)

// 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

// 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';

2. YUI compressor on asteroids!

2.1. FIXED BUGS still present in the original YUI compressor

  • 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 to background:0; anymore. See issue #2528127.
  • text-shadow: 0 0 0; is not compressed to text-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 in calc() 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 or 0ms.
  • Fixes replacing 0 length values in selectors such as .span0px { ... }.

2.2. ENHANCEMENTS over the original YUI compressor

  • Numbers & units compression:
    • Sign is removed from positive numbers: +2em gets minified to 2em.
    • 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 to 0.
    • Added newer unit lengths ch, rem, vw, vh, vm, vmin so we can replace 0rem or 0vw with 0.
  • Colors compression:
    • Percentage and negative RGB values are supported i.e. rgb(100%, 0%, 0%) gets minified to red.
    • HSL colors are compressed too, i.e. hsl(0, 100%, 50%) gets minified to red. HSL angles are wrapped and values are clipped if needed.
  • All CSS properties are lowercased.

3. Unit Tests

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 5.0.0+ installed.
  • 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.

4. API Reference

__construct([ bool $raise_php_limits ])


Class constructor, creates a new CSSmin object.



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.

run(string $css [, int $linebreak_pos ])


Minifies a string of uncompressed CSS code. run() may be called multiple times on a single CSSmin instance.



A string of uncompressed CSS code. Defaults to an empty string ''.


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.

set_memory_limit(mixed $limit)


Sets the memory_limit configuration option for this script

CSSmin default value: 128M



Values & notes: memory_limit documentation

set_max_execution_time(int $seconds)


Sets the max_execution_time configuration option for this script

CSSmin default value: 60



Values & notes: max_execution_time documentation

set_pcre_backtrack_limit(int $limit)


Sets the pcre.backtrack_limit configuration option for this script

CSSmin default value: 1000000



Values & notes: pcre.backtrack_limit documentation

set_pcre_recursion_limit(int $limit)


Sets the pcre.recursion_limit configuration option for this script.

CSSmin default value: 500000



Values & notes: pcre.recursion_limit documentation

5. Who uses this port

  • 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.

6. Changelog

v2.4.8-p4 22 Sep 2014

v2.4.8-p3 26 Apr 2014

  • Fixed all reported bugs: See issues #11, #13 (first case only) and #14.
  • LESS compiler upgraded to version 1.7.0

v2.4.8-p2 13 Nov 2013

  • 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

v2.4.8-p1 8 Aug 2013

  • Fix for the @keyframes 0% step bug. Tests added.
  • LESS compiler upgraded to version 1.4.1