Created
August 2, 2013 05:42
-
-
Save umidjons/6137737 to your computer and use it in GitHub Desktop.
JSCompressor class to minify JS files (uses JSMin and JSMinPlus libraries)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class JSCompressor { | |
| const COMPRESS_METHOD_JSMIN = 1; | |
| const COMPRESS_METHOD_JSMIN_PLUS = 2; | |
| public static function minify( $file, $newfile = null, $compress_method = self::COMPRESS_METHOD_JSMIN, $debug = false ) { | |
| if ( $debug ) | |
| $time_start = microtime( true ); | |
| switch($compress_method) | |
| { | |
| case self::COMPRESS_METHOD_JSMIN: | |
| require( 'min/lib/JSMin.php' ); | |
| $class = 'JSMin'; | |
| break; | |
| case self::COMPRESS_METHOD_JSMIN_PLUS: | |
| require( 'min/lib/JSMinPlus.php' ); | |
| $class = 'JSMinPlus'; | |
| break; | |
| } | |
| if ( !$newfile ) { | |
| $pi = pathinfo( $file ); | |
| $newfile = sprintf( "%s-%s.%s", | |
| $pi[ 'filename' ], | |
| strtolower( $class ), | |
| $pi[ 'extension' ] ); | |
| } | |
| if ( file_exists( $file ) ) { | |
| $source = file_get_contents( $file ); | |
| $minsource = $class::minify( $source ); | |
| file_put_contents( $newfile, $minsource ); | |
| } | |
| if ( $debug ) { | |
| $time_end = microtime( true ); | |
| $time = $time_end - $time_start; | |
| printf( "<p>Minify Method: %s<br>Minify Time: %f<br>File Size: %d<br>Minified File Size: %d</p>", $class, $time, @filesize( $file ), @filesize( $newfile ) ); | |
| } | |
| } | |
| } | |
| JSCompressor::minify( 'jquery-1.10.2.js', '', JSCompressor::COMPRESS_METHOD_JSMIN, true ); // JSMin | |
| JSCompressor::minify( 'jquery-1.10.2.js', '', JSCompressor::COMPRESS_METHOD_JSMIN_PLUS, true ); // JSMinPlus |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment