PHPackages                             bennettstone/magicmin - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bennettstone/magicmin

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bennettstone/magicmin
=====================

MagicMin is a PHP based javascript and stylesheet minification and merging class designed to generate minified, merged, and automatically updating files to simplify the process of minified file usage and updating when going between production and development environments.

495.4k↓26.7%14[5 issues](https://github.com/bennettstone/magic-min/issues)PHP

Since Jun 14Pushed 9y ago12 watchersCompare

[ Source](https://github.com/bennettstone/magic-min)[ Packagist](https://packagist.org/packages/bennettstone/magicmin)[ RSS](/packages/bennettstone-magicmin/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Magic Minifier
==============

[](#magic-minifier)

MagicMin is a PHP based javascript and stylesheet minification and merging class.

This class is designed to generate minified, merged, and automatically updating files to simplify the process of minified file usage and updating when going between production and development environments.

This class has four primary functions:

1. Minification of single files
    - $class-&gt;minify( 'sourcefile', 'outputfile', 'version' );
2. Merging and minifying the contents of whole directories
    - $class-&gt;merge( 'outputfile', 'directory', 'type \[css, js\]', array( 'items to exclude' ), array( 'files to merge and minify in order' ) );
    - The output and minification may be specifically ordered by using the fifth and last parameter, with files passed as an array
    - **Only the files necessary to be prioritized here, and not every file in the directory**
3. Encoding image file data, replacing external image references within CSS
    - Only applies to CSS files
    - Default is false (image references are retained) (See "Basic Usage, item #1")
4. Providing generated assets using gzip with specified cache control
    - zlib Must exist and be enabled, otherwise no gzip will be used
    - Default expires set to 30 days (60 x 60 x 24 x 31)

**This class uses filemtime to determine if and when the minified version should be recreated, and will only create a new minified file IF a file selected for inclusion in the minify or merge functions is newer than the previously created minified file**

Files that contain ".min." in the filename will not have their contents minified, but will still have their contents returned and added to compiled files as normal, as it SHOULD be assumed that those files have already been minified.

Full usage examples are included in example.php, and this package is included with the jqueryui styles in /base, as well as a few misc javascript and bootstrap files for testing.

Regular expressions and str\_replace operators were removed 15-Jun-2013 for javascript minification due to inconsistencies, and have been replaced with:

1. [JsMin](https://github.com/rgrove/jsmin-php) as the default
    - If the jsmin.php file is not found in the same directory as class.magic-min.php, it will be retrieved from github and created in the same directory automatically
    - JsMin is the default minification package used for javascript
2. [Google Closure](https://developers.google.com/closure/compiler/)
    - To use google closure, add 'closure' =&gt; true to the class initation

\##Basic Usage First, include and initiate the class. The class has been updated to use an array with up to 7 key -&gt; value pairs, all accept boolean values or can be omitted entirely:

1. Base64 encoded images (**local or remote**) can automatically replace file references during generation. This applies only to CSS files.
    - 'encode' =&gt; true/false (default is false)
    - url() type file paths beginning with "http" or "https" are retrieved and encoded using cURL as opposed to file\_get\_contents for local files
2. Echo the resulting generated file path, or return to use as a variable
    - 'echo' =&gt; true/false (default is true)
3. Output the total execution time
    - 'timer' =&gt; true/false (default is false)
    - Set as part of \_\_destruct to log to the javascript console, adjust as necessary
4. Output minified/merged assets using gzip with cache control
    - 'gzip' =&gt; true/false (default is false)
5. Use the Google Closure API as opposed to jsmin (jsmin is default)
    - 'closure' =&gt; true (default is false)
6. Retain or remove comments within file contents (Thanks to [muertet](https://github.com/muertet) for this one)
    - 'remove\_comments' =&gt; true/false (defaults to true)
7. Generate hashed filenames based on file generation time. Allows for better cachebusting without the use of query string version numbers
    - 'hashed\_filenames' =&gt; false/true (defaults to false)
8. Automatically output the results of operations taken by MagicMin to the javascript console. Same as calling $minified-&gt;logs(), but without needing to add an explicit call to the "logs()" function
    - 'output\_log' =&gt; false/true (defaults to false)
9. Brute force recompile merged and minified files for use during development
    - 'force\_rebuild' =&gt; false/true (default false, use sparingly!)

```
require( 'class.magic-min.php' );

//Default usage will echo from function calls and leave images untouched
$minified = new Minifier();

//Return data without echo
$minified = new Minifier( array( 'echo' => false ) );

//Echo the resulting file path, while base64_encoding images and including as part of css
$minified = new Minifier( array( 'encode' => true ) );

//Return only AND encode/include graphics as part of css (gasp)
$minified = new Minifier( array( 'encode' => true, 'echo' => false ) );

//Include images as part of the css, and gzip
$minified = new Minifier( array( 'encode' => true, 'gzip' => true ) );

//Use google closure API, and gzip the resulting file
$minified = new Minifier( array( 'gzip' => true, 'closure' => true ) );

//Use google closure API, gzip the resulting file, hash filenames, output to console
$minified = new Minifier(
    array(
        'gzip' => true,
        'closure' => true,
        'hashed_filenames' => true,
        'output_log' => true
    )
);
```

Output a single minified stylesheet

```
