<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/filters.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'filters.compression.php',
    1 => 'Compression Filters',
    2 => 'Compression Filters',
  ),
  'up' => 
  array (
    0 => 'filters.php',
    1 => 'List of Available Filters',
  ),
  'prev' => 
  array (
    0 => 'filters.convert.php',
    1 => 'Conversion Filters',
  ),
  'next' => 
  array (
    0 => 'filters.encryption.php',
    1 => 'Encryption Filters',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'appendices/filters.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="filters.compression" class="section">
  <h2 class="title">Compression Filters</h2>

  <p class="simpara">
   While the <a href="wrappers.compression.php" 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>not</em> generate headers and trailers
    used by command line utilities such as <code class="literal">gzip</code>.  They only compress
    and decompress the payload portions of compressed data streams.
   </span>
  </p></blockquote>

  <div class="section" id="filters.compression.zlib">
   <h2 class="title">zlib.deflate and zlib.inflate</h2>
   <p class="simpara">
    <code class="literal">zlib.deflate</code> (compression) and
    <code class="literal">zlib.inflate</code> (decompression) are implementations of
    the compression methods described in <a href="https://datatracker.ietf.org/doc/html/rfc1951" class="link external">&raquo;&nbsp;RFC 1951</a>.
    The <code class="literal">deflate</code> filter takes up to three parameters passed as
    an associative array.  

    <code class="parameter">level</code> 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).

    <code class="parameter">window</code> 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 <code class="parameter">window</code> size is currently <code class="literal">15</code>.

    <code class="parameter">memory</code> 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 if
    <a href="ref.zlib.php" class="link">zlib</a> support is enabled.
   </p>

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

   </div>

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

   </div>
  </div>

  <div class="section" id="filters.compression.bzip2">
   <h2 class="title">bzip2.compress and bzip2.decompress</h2>
   <p class="simpara">
    <code class="literal">bzip2.compress</code> and
    <code class="literal">bzip2.decompress</code>
    work in the same manner as the zlib filters described above.

    The <code class="literal">bzip2.compress</code> filter accepts up to two parameters given as 
    elements of an associative array: 

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

    <code class="parameter">work</code> 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 <code class="literal">bzip2.decompress</code> filter only accepts one parameter,
    which can be passed as either an ordinary boolean value, or as the 
    <code class="parameter">small</code> element of an associative array.

    <code class="parameter">small</code>, when set to a <strong><code><a href="reserved.constants.php#constant.true">true</a></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 if
     <a href="ref.bzip2.php" class="link">bz2</a> support is enabled.
   </p>

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

   </div>
  </div>
  </div><?php manual_footer($setup); ?>