简体   繁体   中英

Compressing dynamic HTML generated by PHP

I am using PHP for creating some dynamic websites. I have coming across various optimization techniques to optimie my web pages. One among them is HTML compression. Since my web page is having lots of JS, CSS and inline scripts in it. The total size of generated source(HTML) is coming around 150KB.

But if i save the generated page source, and compress it using some sites like http://htmlcompressor.com/compressor.html , the output file is coming around 90KB(where empty line breaks and space are removed). So is there any way to do this dynamically when the page is generated? Is there any helpers or library available for that? Any help on this topic would be greatly appreciated.

One of the best things you can do is remove any inline scripts that don't need to be inline.

I have this at the top of my PHP file:

ob_start();
ob_implicit_flush(0);

Then I include this function: (My notes don't say where I stole it from)

function print_gzipped_page() {
  $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
  if( headers_sent() )
    $encoding = false;
  else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
    $encoding = 'x-gzip';

  // *** I can't recall why I disabled this one ***
  // I had some device that it didn't work with.

  //else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
  //  $encoding = 'gzip';
  else {
    $encoding = false;
  }


  $contents = ob_get_clean();

  if ($encoding)
  {
    $_temp1 = strlen($contents);
    if ($_temp1 < 2048) {   // no need to waste resources in compressing very little data
      print($contents);
    } else {
      header('Content-Encoding: '.$encoding);
      print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
      $contents = gzcompress($contents, 9);
      $contents = substr($contents, 0, $_temp1);
      print($contents);
    }
  }
  else {
    print($contents);
  }
}

At the very bottom of the page, I just call:

print_gzipped_page();

As the code says, if you have already sent headers -- if you've already sent ANY output, basically, then this code not compress anything for you.

I came across a script called as Dynamic Website Compressor that can compress your 'HTML - Inline CSS - Inline JS' on the FLY ( means every page of your site ).

It do not modify anything in your actual server files or in other words it do not modify your files and no modification is required. I hope it might help you

The website that you have mentioned already has a solution for you. Think of it, instead compressing the HTML output of your PHP files, have them to generate already compressed HTML. In this way, you don't waste extra resources in compressing the output, because you already did.

I use HTMLCompressor to compress WordPress themes PHP files and it works like a charm. Just select x/html + PHP from the code type list.

Note that it does not compress HTML code contained in PHP strings, so if your PHP files generate code in this way:

<?php
echo '<div>';
echo '    <b> Some number is ' . $num . ' </b>';
echo '</div>';
?>

change it to:

<div>
    <b> Some number is <?php echo $num; ?> </b>
</div>

and it will be properly compressed ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM