ala747 / Compress-and-Concatenate-Stylesheets-with-PHP

Requires PHP 5.3 or higher.

Home Page:http://compresscss.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

  1. A path to the single CSS file that should be compressed
  2. 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();

Compress a Single Stylesheet

require 'CSS_Compress.php';
$C = new CSS_Compress( 'style.css');
$C->save();

...compressed stylesheet is saved to style_min.css

Compress and Overwrite Your Existing Stylesheet

require 'CSS_Compress.php';
$C = new CSS_Compress( 'style.css', 'styles.css' );
$C->save();

Compress, Concatenate, and Save Three Stylesheets

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();

There's Also An API...

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

Compress Style.css, and Save it as all_min.css.

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.

Compress + Concatenate Multiple Files

$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)))
);

Compress Externally Hosted Stylesheets, Concatenate Them, and Save Locally to style_min.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")
);

About

Requires PHP 5.3 or higher.

http://compresscss.com


Languages

Language:PHP 100.0%