All of the above compressors have very good compression level, but the main limitation is that we have to run it using command prompt.
So, to use that compressor within PHP environment, we need to use
exec statement that needs access rights to the command prompt, so if we don’t have it, we can use this alternative.
This compressor developed by Matthias Mullie and is still actively developed to date, also it has a pretty good compression results too. To use it, follow the following steps:
- Create a working folder inside htdocs directory, for example: I create a folder named minifier.
- Install the library. Installation can be done automatically using composer, if you don’t use composer, you can download it manually through it’s Github page
- Next, extract the files to the directory that we have been created earlier, note that the data and src folder must be located in the same directory. In this example, I put it in the htdocs
$path = 'src'; require_once $path . '/minify/src/Minify.php'; require_once $path . '/minify/src/JS.php'; require_once $path . '/minify/src/Exception.php'; use MatthiasMullie\Minify; $minifier = new Minify\JS('jquery-1.12.4.js'); $minifier->minify('jquery-1.12.4.min.js');
Save the code as php file e.g.
The file size is reduced by 48.8%
The second library that we can use is JShrink. Until this article was written, the latest commit was on June 4, 2015, almost a year, fairly updated.
This library contains only a single php file, however, it does the job well, the compressed code is working and compressed file size is decent. To use it follow the following steps:
- Download Minifier.php file from Github and copy it into the src directory (htdocs\minifier\src).
- Open your code editor, eg notepad++, then copy paste the following code:
include('src\jsshrink.php'); $jsCode = file_get_contents('jquery-1.12.4.js'); $minifiedCode = \JShrink\Minifier::minify($jsCode, array('flaggedComments' => false)); file_put_contents('jquery-1.12.4.min.js', $minifiedCode);
minify_jsshrink.php) and place it into minifier directory.
- Run the php file in a browser (http://localhost/minifier/minify_jsshrink.php). The result is:
The file size is reduced by 48.1%, but it 2kb larger than the previous compressor.
The third library that we can use is JSQueezee. This library is also just a single php file and is still actively developed.
- Download jssqueeze.php file from Github then copy it into the src directory (htdocs\minifier\src)
- As in the previous example, place the jquery-1.12.4.js file into minifier directory.
- Open your code editor, e.g: notepad ++ and put the following code:
$jz = new JSqueeze(); $jsCode = file_get_contents('jquery-1.12.4.js'); $minifiedJs = $jz->squeeze( $jsCode, true, // $singleLine false, // $keepImportantComments false // $specialVarRx ); file_put_contents('jquery-1.12.4.min.js', $minifiedJs);
minify_jssqueeze.php) and place it in the minifier directory.
- Open a browser and run that file (http://localhost/minifier/minify_jsqueeze.php). The result is:
Wow, the file size is reduced by 65.2%, much more compact than the previous. BUT as stated before, this result is not 100% safe, let’s test it
Test whether the compressed file is working
To perform the test, simply create a
.html file, for example
test_compression.html, and then put the following simple code:
<!DOCTYPE HTML> <html> <head> <title>Test The JSQueeze's Compressed Code</title> <script src="jquery-1.12.4.min.js"></script> </head> <body> Test The JSQueeze's Compressed Code </body> </html>
Save the file in the minifier directory, then run in a browser, e.g: chrome. Next, open the browser’s Developer Tools then navigate to the Console section, check whether the error message appears.
But like the others, this library only removes comments, spaces, line breaks, and other characters that are not needed, so the resulted file size is the same.
Loves articles on webdevzoom.com? join our newsletter to get quality article right to your inbox. Nothing else, just quality stuff!!!