PHP使用zlib实现页面的gzip压缩输出

<h3 sizcache="1" sizset="22" ><span class="link_title" sizcache="1" sizset="22">PHP使用zlib实现页面的gzip压缩输出</span></h3> <div class="article_manage" sizcache="1" sizset="23" ></div> <div class="article_manage" sizcache="1" sizset="23" ></div> <p ></p> <div id="article_content" class="article_content" sizcache="1" sizset="29" >php中的GZIP(GNU-ZIP)是一种压缩技术。经过GZIP压缩后页面大小可以变为原来的30%甚至更小。这样用户浏览的时候就会感觉很快。<br /> <span class="Apple-converted-space"></span><br /> 要实现GZIP压缩页面需要浏览器和<span ><span class="channel_keylink">服务器</span>共同支持,实际上就是<span class="channel_keylink">服务器</span>压缩,传到浏览器后浏览器解压并解析。浏览器那边不需要我们担心,因为现在绝大多数浏览器都支持解析GZIP过的页面。我们只要把页面在服务器端压缩再输出到浏览器就行了。<br /> <span class="Apple-converted-space"></span><br /> </span> <p><span > 要压缩一个页面,首先要获得要输出的内容。PHP中的ob_start()(ob =&gt; output buffer)函数可以实现这个功能,它可以把程序里准备输出的内容先放到一个叫做&ldquo;缓冲区&rdquo;的地方。这个函数一定要在页面输出之前使用,所以一般把它放在代码的最顶端。</span></p> <p><span ><br /> </span></p> <p><span ><strong>方法一</strong></span></p> <p>用PHP内置方法</p> <p></p> <div class="dp-highlighter bg_php" sizcache="1" sizset="29"> <div class="bar" sizcache="1" sizset="29"> <div class="tools" sizcache="1" sizset="29"><b>[php]</b><span class="Apple-converted-space"></span>view plaincopyprint?</div> </div> <ol class="dp-c"> <li class="alt"><span><span>&lt;?php</span></span></li> <li><span class="keyword">if</span><span>(</span><span class="func">Extension_Loaded</span><span>(</span><span class="string">'zlib'</span><span>))Ob_Start(</span><span class="string">'ob_gzhandler'</span><span>);</span></li> <li class="alt"><span>Header(</span><span class="string">&quot;Content-type:text/html;charset=utf-8&quot;</span><span>);</span></li> <li><span class="comment">//准备一些待压缩的内容<span class="Apple-converted-space"></span></span><span></span></li> <li class="alt"><span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span>&lt;100;</span><span class="vars">$i</span><span>++){</span></li> <li><span></span><span class="func">echo</span><span>(</span><span class="string">'测试:需要压缩的内容'</span><span>);</span></li> <li class="alt"><span>}</span></li> <li><span class="keyword">if</span><span>(</span><span class="func">Extension_Loaded</span><span>(</span><span class="string">'zlib'</span><span>))Ob_End_Flush();</span></li> <li class="alt"><span>?&gt;</span></li> </ol> </div> <br /> <p></p> <p><span ><span ><strong>方法二</strong></span><br /> </span></p> <p>用自写方法实现gzip压缩<br /> </p> <p></p> <div class="dp-highlighter bg_php" sizcache="1" sizset="33"> <div class="bar" sizcache="1" sizset="33"> <div class="tools" sizcache="1" sizset="33"><b>[php]</b><span class="Apple-converted-space"></span>view plaincopyprint?</div> </div> <ol class="dp-c"> <li class="alt"><span><span class="comment">/**</span></span></li> <li><span><span class="comment">*压缩输出方法gzip压缩函数</span></span></li> <li class="alt"><span><span class="comment">*@param$contentmixed要压缩的页面内容</span></span></li> <li><span><span class="comment">*/</span><span></span></span></li> <li class="alt"><span class="keyword">function</span><span>ob_gzip(</span><span class="vars">$content</span><span>)</span></li> <li><span>{</span></li> <li class="alt"><span></span><span class="keyword">if</span><span>(!headers_sent()&amp;&amp;</span><span class="comment">//如果页面头部信息还没有输出<span class="Apple-converted-space"></span></span><span></span></li> <li><span></span><span class="func">extension_loaded</span><span>(</span><span class="string">&quot;zlib&quot;</span><span>)&amp;&amp;</span><span class="comment">//而且zlib扩展已经加载到PHP中<span class="Apple-converted-space"></span></span><span></span></li> <li class="alt"><span></span><span class="func">strstr</span><span>(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">&quot;HTTP_ACCEPT_ENCODING&quot;</span><span>],</span><span class="string">&quot;gzip&quot;</span><span>)){</span><span class="comment">//而且浏览器说它可以接受GZIP的页面<span class="Apple-converted-space"></span></span><span></span></li> <li><span></span><span class="comment">//为准备压缩的内容贴上&quot;此页已压缩&quot;的注释标签,然后用zlib提供的gzencode()函数执行级别为9的压缩,这个参数值范围是0-9,0表示无压缩,9表示最大压缩,当然压缩程度越高越费CPU.<span class="Apple-converted-space"></span></span><span></span></li> <li class="alt"><span></span><span class="vars">$content</span><span>=gzencode(</span><span class="vars">$content</span><span>.</span><span class="string">&quot;此页已压缩&quot;</span><span>,9);</span></li> <li><span></span></li> <li class="alt"><span></span><span class="comment">//然后用header()函数给浏览器发送一些头部信息,告诉浏览器这个页面已经用GZIP压缩过了!<span class="Apple-converted-space"></span></span><span></span></li> <li><span>header(</span><span class="string">&quot;Content-Encoding:gzip&quot;</span><span>);</span></li> <li class="alt"><span>header(</span><span class="string">&quot;Vary:Accept-Encoding&quot;</span><span>);</span></li> <li><span>header(</span><span class="string">&quot;Content-Length:&quot;</span><span>.</span><span class="func">strlen</span><span>(</span><span class="vars">$content</span><span>));</span></li> <li class="alt"><span>}</span></li> <li><span></span></li> <li class="alt"><span></span><span class="keyword">return</span><span></span><span class="vars">$content</span><span>;</span><span class="comment">//返回压缩的内容<span class="Apple-converted-space"></span></span><span></span></li> <li><span>}</span></li> </ol> </div> 调用方法:<span ><span >给</span>ob_start</span>方法传入<span >ob_gzip</span>作为callback参数,这样当内容进入缓冲区后PHP就会调用ob_gzip函数把它压缩了,然后用ob_end_flush()方法刷新缓冲区,输出内容 <p></p> <p></p> <div class="dp-highlighter bg_php" sizcache="1" sizset="37"> <div class="bar" sizcache="1" sizset="37"> <div class="tools" sizcache="1" sizset="37"><b>[php]</b><span class="Apple-converted-space"></span>view plaincopyprint?</div> </div> <ol class="dp-c"> <li class="alt"><span><span>&lt;?php</span></span></li> <li><span class="comment">//启用一个带有ob_gzip压缩机的工作台<span class="Apple-converted-space"></span></span><span></span></li> <li class="alt"><span>ob_start(</span><span class="string">'ob_gzip'</span><span>);</span></li> <li><span></span></li> <li class="alt"><span class="comment">//准备一些待压缩的内容<span class="Apple-converted-space"></span></span><span></span></li> <li><span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span>&lt;100;</span><span class="vars">$i</span><span>++){</span></li> <li class="alt"><span></span><span class="func">echo</span><span>(</span><span class="string">'测试:需要压缩的内容'</span><span>);</span></li> <li><span>}</span></li> <li class="alt"><span></span></li> <li><span class="comment">//输出压缩成果<span class="Apple-converted-space"></span></span><span></span></li> <li class="alt"><span>ob_end_flush();</span></li> <li><span></span></li> <li class="alt"><span class="comment">/**</span></li> <li><span><span class="comment">*压缩输出方法gzip压缩函数</span></span></li> <li class="alt"><span><span class="comment">*@param$contentmixed要压缩的页面内容</span></span></li> <li><span><span class="comment">*/</span><span></span></span></li> <li class="alt"><span class="keyword">function</span><span>ob_gzip(</span><span class="vars">$content</span><span>)</span></li> <li><span>{</span></li> <li class="alt"><span></span><span class="keyword">if</span><span>(!headers_sent()&amp;&amp;</span><span class="comment">//如果页面头部信息还没有输出<span class="Apple-converted-space"></span></span><span></span></li> <li><span></span><span class="func">extension_loaded</span><span>(</span><span class="string">&quot;zlib&quot;</span><span>)&amp;&amp;</span><span class="comment">//而且zlib扩展已经加载到PHP中<span class="Apple-converted-space"></span></span><span></span></li> <li class="alt"><span></span><span class="func">strstr</span><span>(</span><span class="vars">$_SERVER</span><span>[</span><span class="string">&quot;HTTP_ACCEPT_ENCODING&quot;</span><span>],</span><span class="string">&quot;gzip&quot;</span><span>)){</span><span class="comment">//而且浏览器说它可以接受GZIP的页面<span class="Apple-converted-space"></span></span><span></span></li> <li><span></span><span class="comment">//为准备压缩的内容贴上&quot;此页已压缩&quot;的注释标签,然后用zlib提供的gzencode()函数执行级别为9的压缩,这个参数值范围是0-9,0表示无压缩,9表示最大压缩,当然压缩程度越高越费CPU.<span class="Apple-converted-space"></span></span><span></span></li> <li class="alt"><span></span><span class="vars">$content</span><span>=gzencode(</span><span class="vars">$content</span><span>.</span><span class="string">&quot;此页已压缩&quot;</span><span>,9);</span></li> <li><span></span></li> <li class="alt"><span></span><span class="comment">//然后用header()函数给浏览器发送一些头部信息,告诉浏览器这个页面已经用GZIP压缩过了!<span class="Apple-converted-space"></span></span><span></span></li> <li><span>header(</span><span class="string">&quot;Content-Encoding:gzip&quot;</span><span>);</span></li> <li class="alt"><span>header(</span><span class="string">&quot;Vary:Accept-Encoding&quot;</span><span>);</span></li> <li><span>header(</span><span class="string">&quot;Content-Length:&quot;</span><span>.</span><span class="func">strlen</span><span>(</span><span class="vars">$content</span><span>));</span></li> <li class="alt"><span>}</span></li> <li><span></span></li> <li class="alt"><span></span><span class="keyword">return</span><span></span><span class="vars">$content</span><span>;</span><span class="comment">//返回压缩的内容<span class="Apple-converted-space"></span></span><span></span></li> <li><span>}</span></li> <li class="alt"><span>?&gt;</span></li> </ol> </div> <span ><br /> 经过实际<span class="channel_keylink">测试</span>,用firebug可以看到</span> <p></p> <p><span >如果不启用GZIP需要传输<span >2.8KB=<code class="focusRow subFocusRow "><font face="NSimsun">2900</font></code>B</span>的数据</span></p> <p><span >启用GZIP后缩小为83B</span></p> </div>
RangeTime:0.006039s
RangeMem:215.59 KB
返回顶部 留言