Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 1963

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Compression Filters</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filters.convert.html">Conversion Filters</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="filters.encryption.html">Encryption Filters</a></div>
 <div class="up"><a href="filters.html">List of Available Filters</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="filters.compression" class="section">
  <h2 class="title">Compression Filters</h2>

  <p class="simpara">
   While the <a href="wrappers.compression.html" class="link">Compression Wrappers</a>
   provide a way of creating
   gzip and bz2 compatible files on the local filesystem, they do not provide a 
   means for generalized compression over network streams, nor do they provide a 
   means to begin with a non-compressed stream and transition to a compressed one.  
   For this, a compression filter may be applied to any stream resource at any time.
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Compression filters do <em class="emphasis">not</em> generate headers and trailers
    used by command line utilities such as <em>gzip</em>.  They only compress
    and decompress the payload portions of compressed data streams.
   </span>
  </p></blockquote>

  <p class="simpara">
   <em>zlib.deflate</em> (compression) and
   <em>zlib.inflate</em> (decompression) are implementations of
   the compression methods described in <a href="http://www.faqs.org/rfcs/rfc1951" class="link external">&raquo;&nbsp;RFC 1951</a>.
   The <em>deflate</em> filter takes up to three parameters passed as
   an associative array.  

   <em><code class="parameter">level</code></em> describes the compression
   strength to use (1-9).  Higher numbers will generally yield smaller payloads at
   the cost of additional processing time.  Two special compression levels also exist:
   0 (for no compression at all), and -1 (zlib internal default -- currently 6).

   <em><code class="parameter">window</code></em> is the base-2 log of the compression loopback window size.
   Higher values (up to 15 -- 32768 bytes) yield better compression at a cost of memory,
   while lower values (down to 9 -- 512 bytes) yield worse compression in a smaller memory footprint.
   Default <em><code class="parameter">window</code></em> size is currently <strong><code>15</code></strong>.

   <em><code class="parameter">memory</code></em> is a scale indicating how much work memory should be allocated.
   Valid values range from 1 (minimal allocation) to 9 (maximum allocation).  This memory allocation
   affects speed only and does not impact the size of the generated payload.
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Because compression level is the most commonly used parameter, it may be alternatively
    provided as a simple integer value (rather than an array element).
   </span>
  </p></blockquote>

  <p class="simpara">
    zlib.* compression filters are available with PHP as of version <em>5.1.0</em> if
    <a href="ref.zlib.html" class="link">zlib</a> support is enabled.  They are also available as a backport in version
    <em>5.0.x</em> by installing the <a href="http://pecl.php.net/package/zlib_filter" class="link external">&raquo;&nbsp;zlib_filter</a>
    package from <a href="http://pecl.php.net/" class="link external">&raquo;&nbsp;PECL</a>.  These filters are <em class="emphasis">not</em>
    available for PHP 4.
  </p>

  <div class="example" id="example-5636">
   <p><strong>Example #1 
    <em>zlib.deflate</em> and
    <em>zlib.inflate</em>
   </strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$params&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'level'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'window'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">15</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'memory'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">9</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$original_text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;a&nbsp;test.\nThis&nbsp;is&nbsp;only&nbsp;a&nbsp;test.\nThis&nbsp;is&nbsp;not&nbsp;an&nbsp;important&nbsp;string.\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;original&nbsp;text&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$original_text</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;characters&nbsp;long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.deflated'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'zlib.deflate'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$params</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$original_text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;compressed&nbsp;file&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.deflated'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;bytes&nbsp;long.\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;original&nbsp;text&nbsp;was:\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;Use&nbsp;readfile&nbsp;and&nbsp;zlib.inflate&nbsp;to&nbsp;decompress&nbsp;on&nbsp;the&nbsp;fly&nbsp;*/<br /></span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://filter/zlib.inflate/resource=test.deflated'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Generates&nbsp;output:<br /><br />The&nbsp;original&nbsp;text&nbsp;is&nbsp;70&nbsp;characters&nbsp;long.<br />The&nbsp;compressed&nbsp;file&nbsp;is&nbsp;56&nbsp;bytes&nbsp;long.<br />The&nbsp;original&nbsp;text&nbsp;was:<br />This&nbsp;is&nbsp;a&nbsp;test.<br />This&nbsp;is&nbsp;only&nbsp;a&nbsp;test.<br />This&nbsp;is&nbsp;not&nbsp;an&nbsp;important&nbsp;string.<br /><br />&nbsp;*/<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <div class="example" id="example-5637">
   <p><strong>Example #2 
    <em>zlib.deflate</em> simple
   </strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$original_text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;a&nbsp;test.\nThis&nbsp;is&nbsp;only&nbsp;a&nbsp;test.\nThis&nbsp;is&nbsp;not&nbsp;an&nbsp;important&nbsp;string.\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;original&nbsp;text&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$original_text</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;characters&nbsp;long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.deflated'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">/*&nbsp;Here&nbsp;"6"&nbsp;indicates&nbsp;compression&nbsp;level&nbsp;6&nbsp;*/<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'zlib.deflate'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$original_text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;compressed&nbsp;file&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.deflated'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;bytes&nbsp;long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Generates&nbsp;output:<br /><br />The&nbsp;original&nbsp;text&nbsp;is&nbsp;70&nbsp;characters&nbsp;long.<br />The&nbsp;compressed&nbsp;file&nbsp;is&nbsp;56&nbsp;bytes&nbsp;long.<br /><br />&nbsp;*/<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="simpara">
   <em>bzip2.compress</em> and
   <em>bzip2.decompress</em>
   work in the same manner as the zlib filters described above.

   The <em>bzip2.compress</em> filter accepts up to two parameters given as 
   elements of an associative array: 

   <em><code class="parameter">blocks</code></em> is an integer value
   from 1 to 9 specifying the number of 100kbyte blocks of memory to allocate for
   workspace. 

   <em><code class="parameter">work</code></em> is also an integer value ranging from
   0 to 250 indicating how much effort to expend using the normal compression method
   before falling back on a slower, but more reliable method.  Tuning this parameter
   effects only compression speed.  Neither size of compressed output nor memory usage
   are changed by this setting.  A work factor of 0 instructs the bzip library to use
   an internal default.   

   The <em>bzip2.decompress</em> filter only accepts one parameter,
   which can be passed as either an ordinary boolean value, or as the 
   <em><code class="parameter">small</code></em> element of an associative array.

   <em><code class="parameter">small</code></em>, when set to a <strong><code>TRUE</code></strong> value, instructs the bzip library
   to perform decompression in a minimal memory footprint at the cost of speed.
  </p>

  <p class="simpara">
    bzip2.* compression filters are available with PHP as of version <em>5.1.0</em> if
    <a href="ref.bzip2.html" class="link">bz2</a> support is enabled.  They are also available as a backport in version
    <em>5.0.x</em> by installing the <a href="http://pecl.php.net/package/bz2_filter" class="link external">&raquo;&nbsp;bz2_filter</a>
    package from <a href="http://pecl.php.net/" class="link external">&raquo;&nbsp;PECL</a>.  These filters are <em class="emphasis">not</em>
    available for PHP 4.
  </p>

  <div class="example" id="example-5638">
   <p><strong>Example #3 
    <em>bzip2.compress</em> and
    <em>bzip2.decompress</em>
   </strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$param&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'blocks'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">9</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'work'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;original&nbsp;file&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">'LICENSE'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;bytes&nbsp;long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'LICENSE.compressed'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bzip2.compress'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$param</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'LICENSE'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;compressed&nbsp;file&nbsp;is&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">'LICENSE.compressed'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;bytes&nbsp;long.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Generates&nbsp;output:<br /><br />The&nbsp;original&nbsp;text&nbsp;is&nbsp;3288&nbsp;characters&nbsp;long.<br />The&nbsp;compressed&nbsp;file&nbsp;is&nbsp;1488&nbsp;bytes&nbsp;long.<br /><br />&nbsp;*/<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filters.convert.html">Conversion Filters</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="filters.encryption.html">Encryption Filters</a></div>
 <div class="up"><a href="filters.html">List of Available Filters</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>