LessPHP Minified

Less CSS logoRecently, I came across an excellent tool to keep CSS code clean and non-repetitive. Often, large CSS files end up cluttered with declarations like the following:

This sort of coding is clunky and awkward. LESS offers a way to avoid that awkwardness by allowing for nesting, variable declarations, functions and operations, and mixins. These more semantic .less files are then compiled to create functioning .css files. Unfortunately, it uses Javascript, which means (a) significant client-side processing and (b) availability only to users with Javascript enabled.

Fortunately, I was able to find a compiler for LESS that uses PHP, called lessphp (appropriately enough). This allows for greater flexibility. A key feature is the timestamp check that the compiler uses. You give it an input file and a name for an output file. If the output file doesn’t exist, it will create it. If it does exist, it will check whether the input file has been updated more recently than the output file. If it has, it will recompile, and if not, it will just serve up the existing output file. This means that the file is only compiled for the first user that visits after your stylesheet has been changed: a significant improvement over the Javascript version.

However, this didn’t quite suit my needs. I wanted my output CSS file to be minified as well, and lessphp didn’t offer that function. I could have combined two programs, so that lessphp would spit out a CSS file, then a minification script would minify it and serve it up. But that would be clunky, and would involve traversing through the same file twice. For short files, it might not be a problem, but for large CSS files, it could cause issues. So I have instead created my own extension of lessphp, called LessPHP Minified.

LessPHP Minified

For the details about the language and how to use LessPHP Minified to optimize your CSS coding, you should check out the official lessphp documentation. The syntax is entirely the same as lessphp; the only changes that LessPHP Minified makes are in the output of the CSS.

Including LessPHP Minified in Your Project

Download ButtonUsing LessPHP Minified in your project is quite simple. Simply download the zip file, and then copy less_minified.inc.php and lessc.inc.php to your include directory. From there, the easiest way is to insert PHP code as follows:

Remember to replace the require statement with the actual path to less_minified.inc.php, and change the input and output file names as suits your purposes.

If you need more control over the output, you can also create a new instance of LessMinified:

Finally, you can also parse strings of CSS text as follows:

Configuration Options

Mini Me

LessPHP Minified offers several configurable options for how the output CSS is minified. These options are all found at the top of less_minified.inc.php, and can be set to “true” or “false”.

MINIFY_ELEM_LINE

If set to “true”, this will remove line breaks between each CSS element block. If the other minification options are also set to true, all your output CSS will be on one single line. This is set to “false” by default.

MINIFY_ELEM_LIST

If set to “true”, this will remove spaces in between lists of elements. For example:

will become:

MINIFY_PROP_LINE

If set to “true”, this will remove line breaks in between lists of properties. For example:

will become:

Note: Setting this to “false” will not affect blocks with one property that were on one line in the input CSS. For example:

will remain the same.

MINIFY_LAST_SEMI

If set to “true”, this will remove the last semi-colon on every list of properties. For example:

will become:

MINIFY_HEX

If set to “true”, this option will shorten hex color values to three digits when possible. “#ffffff” will become “#fff”, but “#fa2255” will remain “#fa2255”.

MINIFY_UNITS

If set to “true”, this will optimize unit values in several ways. First, any units on a zero value will be dropped: “0px” or “0em” will become simply “0”. Second, any leading zeros before decimal points will be removed: “0.25em” will become “.25em”, and “-0.75em” will become “-.75em”. Third, decimal values will be rounded to two decimal places. This is especially useful when dealing with the built-in LESS
operators, which may result in long decimal strings.

ALWAYS_COMPILE

If set to “true”, LessMinified will always compile a new CSS file, regardless of the timestamp on the input and output files. This is useful during the process of testing CSS, but is not recommended for production purposes.

Note: the same effect can be produced the following way:

License

As with everything else on this site, LessPHP Minified is offered under a Creative Commons license. To summarize, you are free to use, copy, and distribute the script, as long as you retain proper attribution to the author (me). In addition, you must not distribute it for commercial purposes. This is compatible with the original lessphp license, which can be found in the zip file.

5 responses to “LessPHP Minified”

Steve

Any chance of getting this working with the new lessphp v0.3.0 ?

It has some new features:

– compatibility update for lessjs
– changed argument delim to ,
– don’t need a > to delimit mixin path
– added % string escape function
– change string variable interpolation syntax to @{var}
– psuedo classes are not joined automatically when nested
– & can be used anywhere in selector to represent parent
– ~ operator, and e function for unquoting a string

Jeff

Hi Steve,

While I don’t really intend to keep offering upgrades to this tool, my script essentially just extends the original LessPHP class. So if you download the updated “lessc.inc.php” file and drop it in where you see the older one in my zip file, it should work. I tried it quickly just now, ran the script, and nothing seemed to be amiss.

The minification process primarily affects the output to the CSS file, not the processing of the LessCSS code. So differences in the way mixins and strings and such are handled shouldn’t be a problem. Like I said, just update the “lessc.inc.php” file and all should be well.

At any rate, it’s nice to hear that someone’s actually using my script 🙂

Cheers,
Jeff

S

with lessphp 0.3.4 I get this error. So an upgrading is not hasslingfree

Fatal error: Call to undefined method LessMinified::findBlock() in /www/htdocs/LessPHP_Minified/less_minified.inc.php on line 178

Jeff

Thanks, S. Like I said, I am not really actively supporting this tool. From the looks of that error message, they’ve changed the structure of their code in some significant way. So my tool is offered as-is. You’re welcome to modify it if you wish, but I don’t offer support for upgrading to newer versions of lessphp.

Leave a Reply