This is a tool that allows you to compress and concatenate stylesheets with PHP. To use it, download and import the 'CSS_Compress.php' file into your project.
Next, require it:
require 'CSS_Compress.php';
...and create a new instance of the class. When instantiating the class, as the first argument, pass either:
- A path to the single CSS file that should be compressed
- An array of each stylesheet that should be compressed and concatenated.
$C = new CSS_Compress( 'css/style.css' ); // or $C = new CSS_Compress( array('css/style.css', 'css/style2.css') );
You can also pass an optional second parameter which designates where the new compressed stylesheet will be saved. If omitted, it'll use the first file name you pass, and append "_min" to the end of the string.
$C = new CSS_Compress('style.css', 'css/min/style_min.css');
If you wish to overwrite the file completely, simply set the second argument to the same path as the first.
$C = new CSS_Compress('style.css', 'style.css');
Finally, call the save method, and you're done.
$C->save();
require 'CSS_Compress.php'; $C = new CSS_Compress( 'style.css'); $C->save();
...compressed stylesheet is saved to style_min.css
require 'CSS_Compress.php'; $C = new CSS_Compress( 'style.css', 'styles.css' ); $C->save();
require 'CSS_Compress.php'; $C = new CSS_Compress( array('css/style.css', 'css/style2.css', 'css/style3.css'), 'css/min/styles_min.css' ); $C->save();
If you don't want to use these files locally, you can also pass your stylesheets to compresscss.com, which will do the work for you, and return the concatenated + compressed CSS. With this method, you don't need to download any files. Just open a blank PHP file, and paste in...
file_put_contents( 'all_min.css', file_get_contents('http://compresscss.com/api.php?css=' . urlencode(file_get_contents('examples/css/style.css'))) );
Note that, if you're saving to a directory that does not exist, and you're using the API method, you'll need to create the directory first.
$css = array('examples/css/style.css', 'examples/css/style2.css'); array_walk($css, function( &$val ) { $val = file_get_contents( $val ); }); file_put_contents( 'all_min.css', file_get_contents("http://compresscss.com/api.php?css=" . urlencode(implode($css))) );
file_put_contents( 'style_min.css', file_get_contents("http://compresscss.com/api.php?file_names=http://net.tutsplus.com/wp-content/themes/tuts/css/large.css,http://envato.s3.amazonaws.com/widget/widget.css") );