It was however, short, so at least it had that going for it, which my actually awesome new class “MagicMin” does not have (it weighs in at a meager 800ish lines with comments).
“But why go to all this trouble?” you ask? Couple big reasons!
- I’ve been thrown 50,000 character long single-line strings of minified filth waaaaaay too many times. Which I then have to run through a beautifier, only to re-minify, then repeat with the inevitable change (NOOOOOO)
- Performance and the ability to follow normal development workflows. You know- the kind that aren’t mashed into a unusable non-editable nightmare of singlelinedness.
- The ability to do #2 above, and have something smart enough to go, “Oh hey- he just added a line to one of the 18 CSS files included in the ->merge command, let’s make a new beautiful updated file!” without me having to tell it, and without having to do anything (ANYTHING) manually
To preemptively answer the forthcoming questions:
- Yes, I am aware that older versions of IE do not like base64_encoded images
And to preemptively answer the next question (“So, why then?”):
- For fun
- Because I can
- Because mine is more awesome (because it’s mine)
Before you waste time reading ANY further, I feel I should demonstrate.
require( 'class.magic-min.php' ); //Default usage will echo from function calls and leave images untouched $minified = new Minifier();
Now all you need to do is replace the normal references in your script the stylesheet includes…
But now you’re all like, “But, but, but- there are 19 css files in my /css directory and I only want to include 3 of them!”. GOOD.
<?php $include_only = array( 'css/base/jquery.ui.all.css', 'css/base/jquery.ui.base.css', 'css/base/jquery.ui.spinner.css' ); ?> <link href="<?php $minified->merge( 'css/base/specified-files.css', 'css/base', $include_only ); ?>" rel="stylesheet" />
Satisfied? Good. Continue!
That’s right. I said MAGIC. And I meant it.
MagicMin (lame name, yes, descriptive, yes) has four primary functions (lots of bullet points today because I said so!):
- Merging AND minification of groups of files via the ->merge( ‘params below on this page’ ) command with some awesome stuff:
- Files must be the same type (css or js)
- Happily glob’s it’s way through any specified directory, grabbing and minifying any and all files of matched types
- Allows you to glob your way through a directory, excluding any files you deem unworthy
- Allows you to specify an order of the files, and this is only necessary for the files that need to be ordered- all others will be glob’d in after the order parameters have been met
- And last but CERTAINLY not least, allow you to specify ONLY an array of files to include (they really should be the same css or js type if you have any desire for your scripts or stylesheets to work)
- Base64_encoding images into stylesheets to enable all assets to be loaded in a single request (small files [think icons and slivers of backgrounds])
- 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, however, 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).
- The Google Closure API (default, however, a bit slow to initially create the minified files at approximately 7 seconds)
- It’s google. They’re reliable, the minification is topnotch stuff
- JShrink (JShrink.php) is included in the download, however, in the event that it does not exist in the same directory as the class.magic-min.php file, the class will retrieve it from github, write it to a file named JShrink.php in the same directory as the class, and THEN use it.
- This only applies if the class is initiated with ‘closure’ => false
Want some more?! Good! There’s MORE!
<?php $minified->logs(); ?>
For the rest, it’s best if you just read the included documentation, check out the example file (example.php naturally), and add your comments and feature requests below!
|download: MagicMin- PHP Minification class for CSS and JS (120.69KB)|
20 Comments + Add Comment
Leave a comment
- MySQL : Query WordPress Database for Invalid Media Filenames
- PHP : Get Actual IP Address with PHP
- PHP : Clean sanitized database contents on output
- htaccess : Force www. prefix in URIs
- PHP : Force File Download with Correct Content Type
- Wordpress : Disable upgrade notification
- htaccess : Codeigniter Access to JS and CSS Files