简体   繁体   English

压缩PHP生成的动态HTML

[英]Compressing dynamic HTML generated by PHP

I am using PHP for creating some dynamic websites. 我正在使用PHP创建一些动态网站。 I have coming across various optimization techniques to optimie my web pages. 我遇到了各种优化技术来优化我的网页。 One among them is HTML compression. 其中之一是HTML压缩。 Since my web page is having lots of JS, CSS and inline scripts in it. 由于我的网页中包含许多JS,CSS和内联脚本。 The total size of generated source(HTML) is coming around 150KB. 生成的源(HTML)的总大小约为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). 但是,如果我保存生成的页面源,并使用http://htmlcompressor.com/compressor.html之类的某些网站进行压缩,则输出文件大约为90KB(其中的空换行符和空格已删除)。 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: 我在PHP文件的顶部有这个:

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. 如代码所示,基本上,如果您已经发送了标头-如果您已经发送了ANY输出,则此代码不会为您压缩任何内容。

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 ). 我遇到了一个称为动态网站压缩程序的脚本,该脚本可以在FLY上压缩您的“ HTML-内联CSS-内联JS”(意味着您网站的每个页面)。

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. 想一想,而不是压缩PHP文件的HTML输出,而是让它们生成已​​经压缩的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. 我使用HTMLCompressor压缩WordPress主题的PHP文件,它的工作原理很吸引人。 Just select x/html + PHP from the code type list. 只需从代码类型列表中选择x / html + PHP

Note that it does not compress HTML code contained in PHP strings, so if your PHP files generate code in this way: 请注意,它不会压缩PHP字符串中包含的HTML代码,因此,如果您的PHP文件以这种方式生成代码:

<?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 ;) 它将被正确压缩;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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