How to Minify Js in PHP Easily...Or Something Else

How to minify JS in PHP easily...Or something else

I have used a PHP implementation of JSMin by Douglas Crockford for quite some time. It can be a little risky when concatenating files, as there may be a missing semicolon on the end of a closure.

It'd be a wise idea to cache the minified output and echo what is cached so long as it's newer than the source file.

require 'jsmin.php';

if(filemtime('scripts_template.js') < filemtime('scripts_template.min.js')) {
read_file('scripts_template.min.js');
} else {
$output = JSMin::minify(file_get_contents('scripts_template.js'));
file_put_contents('scripts_template.min.js', $output);
echo $output;
}

You could also try JShrink. I haven't ever used it before, since I haven't had difficulty with JSMin before, but this code below should do the trick. I hadn't realized this, but JShrink requires PHP 5.3 and namespaces.

require 'JShrink/Minifier.php';

if(filemtime('scripts_template.js') < filemtime('scripts_template.min.js')) {
read_file('scripts_template.min.js');
} else {
$output = \JShrink\Minifier::minify(file_get_contents('scripts_template.js'));
file_put_contents('scripts_template.min.js', $output);
echo $output;
}

How to minify php page html output?

CSS and Javascript

Consider the following link to minify Javascript/CSS files: https://github.com/mrclay/minify

HTML

Tell Apache to deliver HTML with GZip - this generally reduces the response size by about 70%. (If you use Apache, the module configuring gzip depends on your version: Apache 1.3 uses mod_gzip while Apache 2.x uses mod_deflate.)

Accept-Encoding: gzip, deflate

Content-Encoding: gzip

Use the following snippet to remove white-spaces from the HTML with the help ob_start's buffer:

<?php

function sanitize_output($buffer) {

$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/' // Remove HTML comments
);

$replace = array(
'>',
'<',
'\\1',
''
);

$buffer = preg_replace($search, $replace, $buffer);

return $buffer;
}

ob_start("sanitize_output");

?>

How do you minify and/or compress JavaScript that is in a php file?

Your PHP script generates the Javascript code, so it can minify the code before outputting it: generate the code in a variable, then pass that variable to the minifier, and only then output to the browser.

Here's a PHP library for that.

How do i minify my js in my framework

Yahoo (YUI) used to have a compressor/ minifier, but they're now moving to UglifyJS. See:

  • https://github.com/yui/yuglify
  • https://github.com/yui/yuicompressor/

There are also several online minification services, based on the YUI compressor.

  • http://refresh-sf.com/yui/

how to compress / minify javascript that has embedded php

PHP attempts to completely separate the JavaScript in this way. In your PHP file you keep the values as follows:

file.php:

<html>
<head>...</head>
<body>
...
<input type="hidden" id ="value_type" value="<?=$type?>" />
<input type="hidden" id ="value_cert" value="<?=$cert?>" />
<input type="hidden" id ="value_gauge" value="<?=$gauge?>" />
...
</body>
</html>

file.js:

$(function() {

$.data_values = {

"type": $("#value_type").val(),
"cert": $("#value_cert").val(),
"gauge": $("#value_gauge").val()

};

});

when you have to use the values:

$('input[name=type]:radio').filter('[value="'+$.data_values.type +'"]').attr('checked', true);
$('input[name=cert]:checkbox').filter('[value="'+$.data_values.cert +'"]').attr('checked', true);
$('input[name=gauge]:checkbox').filter('[value="'+$.data_values.gauge +'"]').attr('checked', true);


Related Topics



Leave a reply



Submit