PHP zlib Extension Implementation gzip compression output various methods summary _php tutorial

Source: Internet
Author: User
Tags fread
In general, we have a large number of data transmission manager want to reduce the bandwidth of the server pressure, will take a way to compress file transfer, PHP with Zlib can also achieve gzip compression output, below we look at the gzip compression output various methods summary.

GZIP (Gnu-zip) is a compression technique. After gzip compression, the page size can change to 30% or smaller. This way users will feel very happy when they browse!

Preparatory work

1. Can't find the Php_zlib.dll file?

Since php4.3 started zlib compression has been built into PHP, so at least the Windows environment is not required to install zlib.

2. Install and build PHP operating environment

Since the light through the php.ini configuration file to open gzip configuration implementation of PHP gzip compression output is not possible, it needs Apache support, so it is recommended to install PHP+APACHE+MYSQL operating environment.

PHP gzip Configuration steps

First, open the php.ini configuration file, locate zlib.output_compression = Off,


Zlib.output_compression = Off
; zlib.output_compression_level = 1

Revision changed to

Zlib.output_compression = On
Zlib.output_compression_level = 6

Example 1

PHP uses zlib extensions for page gzip compression output

Code

The code is as follows Copy Code

function Ob_gzip ($content)//$content is the page content to compress

{

if (!headers_sent () && extension_loaded ("zlib") && strstr ($_server["http_accept_encoding"], "gzip") Determines whether the page header information is output, whether the zlib extension in PHP has been loaded, whether the browser supports GZIP technology
{
$content = Gzencode ($content. "n//This page is compressed", 9); Paste the "//This page compressed" comment label on the contents of the compressed content, and then use the Gzencode () function provided by zlib to perform a level 9 compression, which ranges from 0-9,0 to no compression, 9 for maximum compression, and, of course, the higher the compression level, the more CPU.
Use the header () function to send some header information to the browser, telling the browser that the page has been compressed with gzip!
Header ("Content-encoding:gzip");
Header ("vary:accept-encoding");
Header ("Content-length:". strlen ($content));
}
return $content; Returns the compressed content

After the function is written, it is called with Ob_start, so the original Ob_start () becomes

Ob_start (' Ob_gzip '); Add a parameter to Ob_start () and the parameter name is just the function name. So when the content enters the buffer, PHP calls the Ob_gzip function to compress it.

Last End Buffer

Ob_end_flush (); End buffer, output content. Of course, this function is not necessary, because the program executes to the end automatically outputs the buffer contents.

The final complete example

The code is as follows Copy Code

Call a function named Ob_gzip to compress the content
Ob_start (' Ob_gzip ');
Output content
Ob_end_flush ();
This is the Ob_gzip function.

function Ob_gzip ($content)
{
if (!headers_sent () &&extension_loaded ("Zlib")
&&strstr ($_server["http_accept_encoding"], "gzip")
{
$content = Gzencode ($content. "n//This page is compressed", 9);
Header ("Content-encoding:gzip");
Header ("vary:accept-encoding");
Header ("Content-length:". strlen ($content));
}
return $content;
}
?>

Example 2

Zlib compression and decompression of SWF file code

Examples of files:

The code is as follows Copy Code
Not added to determine whether the SWF file has been compressed, into the need can be based on the first byte of a file is ' F ' or ' C ' to determine
To compress a SWF file:
//--------------------------------------------------------------------------------------------------
Filename
$filename = "test.swf";
Open File
$rs = fopen ($filename, "R");
Reading data from a file
$str = Fread ($rs, FileSize ($filename));
Set SWF header File
$head = substr ($str, 1,8);
$head = "C". $head;
Get SWF file contents
$body = substr ($STR, 8);
Compress the contents of a file using the highest compression level 9
$body = Gzcompress ($body, 9);
Merging file headers and contents
$str = $head. $body;
Close the Read file stream
Fclose ($RS);
Create a new file
$ws = fopen ("create.swf", "w");
Write a file
Fwrite ($ws, $STR);
Close file leave
Fclose ($WS);
//----------------------------------------------------------------------------------------------------
?>
To extract the SWF file:
//----------------------------------------------------------------------------------------------------
Filename
$filename = "test.swf";
Open File
$rs = fopen ($filename, "R");
Reading data from a file
$str = Fread ($rs, FileSize ($filename));
Set SWF header File
$head = substr ($str, 1,8);
$head = "F". $head;
Get SWF file contents
$body = substr ($STR, 8);
Unzip the contents of a file
$body = gzuncompress ($body);
Merging file headers and contents
$str = $head. $body;
Close the Read file stream
Fclose ($RS);
Create a new file
$ws = fopen ("create.swf", "w");
Write a file
Fwrite ($ws, $STR);
Close file leave
Fclose ($WS);
//----------------------------------------------------------------------------------------------------
?>

Example 3

Turn on PHP zlib (gzip) compression output


PHP gzip Configuration Knowledge Points:

1, the default PHP is not open zlib entire station compression output, but by the need to compress the output of the page using the Ob_gzhandler function, the two can only two select one, or will error.

2, zlib.output_compression default value is off, you can set it to on, or output buffer size (default is 4k)

3, Zlib.output_compression_level represents the compression ratio, the default recommended setting compression ratio of 6, the optional range of 1-9,-1 for closing PHP zlib (gzip) compression

Second, save php.ini configuration file and restart Apache server

Third, open Apache configuration file httpd.conf, configure Mount Deflate_module

This step is the most critical to open the PHP gzip compression output configuration step, many netizens will say I have opened the php.ini configuration file in the PHP gzip configuration how still did not implement PHP gzip compression, is because did not let Apache load Deflate_module, The method is as follows,

The code is as follows Copy Code
#LoadModule Deflate_module modules/mod_deflate.so

Remove the # number from the beginning and restart Apache.


http://www.bkjia.com/PHPjc/444679.html www.bkjia.com true http://www.bkjia.com/PHPjc/444679.html techarticle in general, we have a large number of data transmission manager want to reduce the bandwidth pressure of the server, will take a way to compress the file transfer, PHP with Zlib can also achieve gzip compression output, the following ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.