2


I'm trying to compress HTML code generated by Magento with this:
Observer.php

        public function alterOutput($observer)
    {
        $lib_path = Mage::getBaseDir('lib').'/Razorphyn/html_compressor.php';

        include_once($lib_path);

        //Retrieve html body
        $response = $observer->getResponse();       
        $html     = $response->getBody();

        $html=html_compress($html);

        //Send Response
        $response->setBody($html);
    }

html_compressor.php:

function html_compress($string){

    global $idarray;
    $idarray=array();

    //Replace PRE and TEXTAREA tags
    $search=array(
                    '@(<)\s*?(pre\b[^>]*?)(>)([\s\S]*?)(<)\s*(/\s*?pre\s*?)(>)@',   //Find PRE Tag
                    '@(<)\s*?(textarea\b[^>]*?)(>)([\s\S]*?)(<)\s*?(/\s*?textarea\s*?)(>)@' //Find TEXTAREA
                );
    $string=preg_replace_callback($search,
                                    function($m){
                                        $id='<!['.uniqid().']!>';
                                        global $idarray;
                                        $idarray[]=array($id,$m[0]);
                                        return $id;
                                    },
                                    $string
    );

    //Remove blank useless space
    $search = array(
                    '@( |\t|\f)+@', // Shorten multiple whitespace sequences
                    '@(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+@',   //Remove blank lines
                    '@^(\s)+|( |\t|\0|\r\n)+$@' //Trim Lines
                    );
    $replace = array(' ',"\\1",'');
    $string = preg_replace($search, $replace, $string);

    //Replace IE COMMENTS, SCRIPT, STYLE and CDATA tags
    $search=array(
                    '@<!--\[if\s(?:[^<]+|<(?!!\[endif\]-->))*<!\[endif\]-->@',  //Find IE Comments
                    '@(<)\s*?(script\b[^>]*?)(>)([\s\S]*?)(<)\s*?(/\s*?script\s*?)(>)@',    //Find SCRIPT Tag
                    '@(<)\s*?(style\b[^>]*?)(>)([\s\S]*?)(<)\s*?(/\s*?style\s*?)(>)@',  //Find STYLE Tag
                    '@(//<!\[CDATA\[([\s\S]*?)//]]>)@', //Find commented CDATA
                    '@(<!\[CDATA\[([\s\S]*?)]]>)@'  //Find CDATA
                );
    $string=preg_replace_callback($search,
                                    function($m){
                                        $id='<!['.uniqid().']!>';
                                        global $idarray;
                                        $idarray[]=array($id,$m[0]);
                                        return $id;
                                    },
                                    $string
    );

    //Remove blank useless space
    $search = array(
                    '@(class|id|value|alt|href|src|style|title)=(\'\s*?\'|"\s*?")@',    //Remove empty attribute
                    '@<!--([\s\S]*?)-->@',  // Strip comments except IE
                    '@[\r\n|\n|\r]@', // Strip break line
                    '@[ |\t|\f]+@', // Shorten multiple whitespace sequences
                    '@(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+@', //Remove blank lines
                    '@^(\s)+|( |\t|\0|\r\n)+$@' //Trim Lines
                    );
    $replace = array(' ','',' ',' ',"\\1",'');
    $string = preg_replace($search, $replace, $string);

    //Replace unique id with original tag
    $c=count($idarray);
    for($i=0;$i<$c;$i++){
        $string = str_replace($idarray[$i][0], "\n".$idarray[$i][1]."\n", $string);
    }

    return $string;
}

My main concers are two:

  • Is this a heavy(or good) solution?
  • Is there a way to optimize this?
  • Has it really got sense to compress a Magento HTML page(taken resource and time vs real benefit)?
Razorphyn
  • 1,314
  • 13
  • 37

2 Answers2

1

I will not comment or review your code. Deciphering regexes (in any flavor) is not my favorite hobby.

Yes, compressing HTML makes sense if you aim to provide professional services.

If I look at a HTML code of someone's site with lots of nonsense blank spaces and user-useless comments inside and the site disrespects Google's PageSpeed Insights Rules and does not help in making the web faster and eco-friendly then it says to me: be aware, don't trust, certainly don't give them your credit card number

My advice:

  1. read answers to this question: Stack Overflow: HTML minification?
  2. read other developer's code, this is the one I use: https://github.com/kangax/html-minifier
  3. benchmark, e.g. run Google Chrome > Developer tools > Audits
  4. test a lot if your minifier does not unintentionally destroy the pages
Community
  • 1
  • 1
xmojmr
  • 8,073
  • 5
  • 31
  • 54
0

There is really no point in doing this if you have gzip compression (and you should have it) enabled. It's a waste of CPU cycles really. You should rather focus on image optimization, reducing number of http requests and setting proper cache headers.

Lord Skeletor
  • 531
  • 3
  • 10