Giter Club home page Giter Club logo

compress-and-concatenate-stylesheets-with-php's Introduction

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

compress-and-concatenate-stylesheets-with-php's People

Contributors

jeffreyway avatar

Watchers

James Cloos avatar Rafique avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.