
Originally Posted by
Bootfit
OK - sorry if this is the wrong thread to post in - not sure what the right one would be.
Has anyone had any experience minifying html/css/javascript for optimising for Google page speed?
We're building a site out of .php includes - do we have to manually minify each file or is there something that can minify the whole site on the fly or in one go? Does that make sense?
Same goes for the CSS, javascript etc...
Cheers all
According to Google Page Speed minifying is considered Low Priority. (Don't sweat the petty things and only pet the...)
I use a PHP Framework that fortunately combines all included files into a single string $view.
I also have a $_SESSION variable which toggles compression so the uncompressed $view is easier to DEBUG
Anyway I adapted this Method to suit my requirements.
# Method to Maybe Compress HTML Source
PHP Code:
//==============================================
//
// usage:
// $this->_maybe_compress_using_reference( $view );
//
//==============================================
function _maybe_compress_using_reference( & $result='', $return_result=false )
{
if ( $_SESSION['jjj']['compress_result'] )
{
$_SESSION['zzz_after'] = ''; // strlen($result);
$_SESSION['zzz_before'] = strlen($result);
$search = array
(
'/\>[^\S ]+/s', //strip whitespaces after tags, except space
'/[^\S ]+\</s', //strip whitespaces before tags, except space
'/(\s)+/s' // shorten multiple whitespace sequences
);
$replace = array
(
'>',
'<',
'\\1'
);
$result = preg_replace($search, $replace, $result);
// DOES NOT SHOW ADVERTS ?????
// $hex=array("\x0D","\x0A");
// $result=str_replace($hex,'',$result);
// Optional Cosmetic Stuff to show results at the end of the $view
$_SESSION['zzz_after'] = strlen($result);
# Dab on end
if(LOCALHOST) // DABS this on the end after </body?</html>
{
$result = str_replace("</body></html>",'', $result);
$result .= '<p class=\'clb\'> Crunched output results:'
. jj.js .'Before: ' . number_format($_SESSION['zzz_before'])
. jj.js .'After: '. number_format($_SESSION['zzz_after'])
. jj.js .'Saving: ' . number_format($_SESSION['zzz_before'] - $_SESSION['zzz_after']) .' bytes'
. jj.js .'Percent: '
. number_format(100 * ($_SESSION['zzz_before'] - $_SESSION['zzz_after']) / $_SESSION['zzz_before']) .' %'
. '</p>'
. '</body></html>';
}
}
#RETURNED BY REFERENCE
#return $result;
}//endfunc
Bookmarks