CSS Compressor

April 28, 2009 | Download | Github
Updated: 2011-01-03
Tags: css, php

CSSCompression is a PHP based CSS minifier that analyzes stylesheets for various compressions. It finds possible CSS shorthand techniques for combination of properties.

Usage

require( 'src/CSSCompression.php' );
$compressed = CSSCompression::express( $css, 'sane' );

Or, if you need to run it multiple times

$CSSC = new CSSCompression( 'sane' );
$compressed = $CSSC->compress( $css );

Modes

Modes are pre-defined sets of options that can be set by passing in the mode name.

  • Safe: (99% safe) Safe mode does zero combinations or organizing. It's the best mode if you use a lot of hacks.
  • Sane: (90% safe) Sane mode does most combinations(multiple long hand notations to single shorthand), but still keeps most declarations in their place.
  • Small: (65% safe) Small mode reorganizes the whole sheet, combines as much as it can, and will break most comment hacks.
  • Full: (64% safe) Full mode does everything small does, but also converts hex codes to their short color name alternatives.

Here's a few different ways to initiate a mode.

// Express with safe mode
$compressed = CSSCompression::express( $css, 'safe' );

// Creating new instance with sane mode
$CSSC = new CSSCompression( 'sane' );

// Setting an instance with small mode
$CSSC->mode( 'small' );

// Or compressing with the current instance, and setting full mode
$compressed = $CSSC->compress( $css, 'full' );

Singleton Instances

Yes the compressor provides singleton access(separate from express), but use it wisely.

$CSSC = CSSCompression::getInstance();

// Or, if you want to keep named instances
$rose_instance = CSSCompression::getInstance('rose');
$blue_instance = CSSCompression::getInstance('blue');

Option Handling

The compressor has an option function with multiple functionalities.

  • If no arguments are passed in, the entire options array is returned.
  • If a single name argument is passed, then the value of that key name in the options array is returned.
  • If both a name and value are passed, then that value is set to it's corresponding key in the array.
// Returns entire options array
$options = $CSSC->option();

// Returns the readability value
$readability = $CSSC->option( 'readability' );

// Sets the readability to non-readable
$CSSC->option( 'readability', CSSCompression::READ_NONE );

Alternatively, you can use direct access methods

// Direct access to options array
$options = $CSSC->options;

// Direct access to readability option value
$readability = $CSSC->readability;

// Direct setting of readability value
$CSSC->readability = CSSCompression::READ_NONE;

Additionally, a reset function is provided to revert back to base options (decided at runtime).

// Resets options to original values
$CSSC->reset();

Readability

The compressor class provides static integers that map to the internal readability values

CSSCompression::READ_MAX // Maximum Readability
CSSCompression::READ_MED // Medium readability of output
CSSCompression::READ_MIN // Minimal readability of output
CSSCompression::READ_NONE // No readability of output (full compression into single line)

// To set maximum readability (Assuming you have your own instance)
$CSSC->option( 'readability', CSSCompression::READ_MAX );

// Or, just pass it in as another option
$options = array(
	'readability' => CSSCompression::READ_MAX,
	// Other options ...
);
// Get full readability through express
$compressed = CSSCompression::express( $css, $options );

Contributors

Download

Latest: css-compressor-3.0.zip
Released: January 3, 2011
- Configuration Modes
- pseduo-space, add space between pseduo selectors and comma/brace for ie6
- removing leading 0 of a decimal, 0.633px -> .633px
- handling all At Rules (charsets,media,imports,font-family,etc..)
- handling ms filter paths
- More css hacks support, including box-modal hack
- attr2selector, Convert id=blah and class=blah attributes to their selector counterparts
- strict-id, removes all selectors up to the last id
- add-unknown, adds unknown blocks to top of output in a comment
- Converted to subclass architecture, more modular.
- Using JSON based sandbox specs for focused unit tests.
- Helper conversion definitions(long2hex, hex2short) are now in JSON based files.
- Added benchmark/regression testing

Past Release's:
January 3, 2011: css-compressor-3.0-full.zip
December 19, 2010: css-compressor-3.0beta.zip
December 19, 2010: css-compressor-3.0beta-full.zip
May 1, 2010: css-compressor-2.0.zip
September 5, 2009: css-compressor-1.0.zip
May 13, 2009: css-compressor-r8.zip
May 7, 2009: css-compressor-r6.zip
May 5, 2009: css-compressor-r5.zip
April 28, 2009: css-compressor-r4.zip

Have a question or comment? ask@codenothing.com.