CSS Compressor
After running some tests against the Icey compressor, I realized it didn't do some of the things that I wanted it to do. So I decided to go ahead and write one from scratch that I can customize to my needs.
Now I realize that there are plenty of other css compressors out there, and all of them have their own unique compressioin algorithims. I went with the object-oriented approach, for two reasons.
1) For me, OO-PHP just has this flow to it that makes it easier to understand.
2) I can easily latch on more functions when needed without worrying about name confliction issues.
Within the script, I made good use of the preg_replace function matched while using strings returned from the preg_match_all function. The selectors and properties contained within are seperated out early on for group compresssions.
When the script finds properties that it can combine, such as:
p {
margin-top: 10px;
margin-right: 15px;
margin-bottom: 5px;
margin-left: 20px;
}
It will combine these 4 properties into a single definition, and replace each line like so:
p {
margin:10px 15px 5px 20px;
margin:10px 15px 5px 20px;
margin:10px 15px 5px 20px;
margin:10px 15px 5px 20px;
}
And the last function called at the end of the script will go through and remove multiple definitions like the one above, and transform the selector definition into the following:
p {
margin:10px 15px 5px 20px;
}
So it is very important to keep the 'Remove multiply defined properties' option enabled, to remove the redundant transformations created from the script.
It should be noted that you should always ensure your code is W3C Complient before compressing it, it might bring some unexpected results.
I have provided an online version of the script here, which will always use the latest release. You can also navigate through that page for news on updates and latest releases for your own personal testing as you see fit.
Download
Latest: css-compressor-2.0.zipReleased: May 1, 2010
-Look behind regexs are used to not break urls
-Singleton access added
-Exposing byte unit conversion display
-The compressor no longer self instantiates itself, you must explicitly initialize it.
-Most methods that were never meant to be public, have been turned into private/protected based on accessibility requirements.
Past Release's:
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
RSS