Summary of various methods for GZIP compression output by zlib extension in PHP

Source: Internet
Author: User

In general, we have a lot of data transmission mechanisms to reduce the bandwidth pressure on the server. We will adopt a method to compress file transmission. in php, zlib can also achieve gzip compression output, the following is a summary of GZIP compression output methods.

GZIP (GNU-ZIP) is a compression technology. After GZIP compression, the page size can be changed to 30% or even smaller. In this way, users will feel very happy when Browsing!

Preparations

1. cannot find the php_zlib.dll file?

Zlib compression has been built into php since php4.3, so at least zlib does not need to be installed in Windows.

2. Install and build the php Runtime Environment

Since gzip configuration enabled in the php. ini configuration file cannot be used to compress php gzip output, it requires apache support. Therefore, we recommend that you install and build a php + apache + mysql runtime environment.

Php gzip configuration steps

1. Open the php. ini configuration file and find zlib. output_compression = Off.


Zlib. output_compression = Off
; Zlib. output_compression_level =-1

Change

Zlib. output_compression = On
Zlib. output_compression_level = 6

 

Instance 1

PHP uses zlib extension to implement GZIP compressed page output

Code

The Code is as follows: Copy code

Function ob_gzip ($ content) // $ content is the page content to be compressed.

{

If (! Headers_sent () & extension_loaded ("zlib") & strstr ($ _ SERVER ["HTTP_ACCEPT_ENCODING"], "gzip") // determines whether the page header information is output, whether zlib extensions in PHP have been loaded, and whether the browser supports GZIP Technology
{
$ Content = gzencode ($ content. "n // this page has been compressed", 9); // paste the "// this page has been compressed" annotation label for the content to be compressed, and then use the gzencode () provided by zlib () the function execution level is 9. The value range of this parameter is 0-9. 0 indicates no compression, and 9 indicates the maximum compression. Of course, the higher the compression level, the higher the CPU cost.
// 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; // return Compressed content

After writing the function, call it with ob_start, so the original ob_start () becomes

Ob_start ('ob _ gzip '); // Add a parameter to ob_start (). The parameter name is the name of the function. In this way, when the content enters the buffer, PHP will call the ob_gzip function to compress it.

Final end Buffer

Ob_end_flush (); // end the buffer and output the content. Of course, this function is fine, because the buffer content is automatically output after the program is executed.

Final complete instance

The Code is as follows: Copy code

<? Php
// 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;
}
?>

Instance 2

Zlib code for compressing and decompressing swf files

File example:

The Code is as follows: Copy code
// If the swf file is not compressed, the first byte of the file is 'f' or 'C '.
Compress the swf file:
// Configure //--------------------------------------------------------------------------------------------------
// File name
$ Filename = "test.swf ";
// Open the file
$ Rs = fopen ($ filename, "r ");
// Read the file data
$ Str = fread ($ rs, filesize ($ filename ));
// Set the swf header file
$ Head = substr ($ str, 1, 8 );
$ Head = "C". $ head;
// Obtain the swf File Content
$ Body = substr ($ str, 8 );
// Compress the file content with a maximum compression level of 9
$ Body = gzcompress ($ body, 9 );
// Merge the file header and content
$ Str = $ head. $ body;
// Close the Read File stream
Fclose ($ rs );
// Create a new file
$ Ws = fopen ("create.swf", "w ");
// Write an object
Fwrite ($ ws, $ str );
// Close file retention
Fclose ($ ws );
// Configure //----------------------------------------------------------------------------------------------------
?>
Decompress the swf file:
// Configure //----------------------------------------------------------------------------------------------------
// File name
$ Filename = "test.swf ";
// Open the file
$ Rs = fopen ($ filename, "r ");
// Read the file data
$ Str = fread ($ rs, filesize ($ filename ));
// Set the swf header file
$ Head = substr ($ str, 1, 8 );
$ Head = "F". $ head;
// Obtain the swf File Content
$ Body = substr ($ str, 8 );
// Decompress the File Content
$ Body = gzuncompress ($ body );
// Merge the file header and content
$ Str = $ head. $ body;
// Close the Read File stream
Fclose ($ rs );
// Create a new file
$ Ws = fopen ("create.swf", "w ");
// Write an object
Fwrite ($ ws, $ str );
// Close file retention
Fclose ($ ws );
// Configure //----------------------------------------------------------------------------------------------------
?>

Instance 3

Enable php zlib (gzip) compression output


Php gzip configuration knowledge point:

1. By default, php does not enable zlib for full-site compression output. Instead, it uses the ob_gzhandler function for the page for compression output. You can only select either of the two. Otherwise, an error is reported.

2. The default value of zlib. output_compression is Off. You can set it to On or output buffer size (4 k by default)

3. zlib. output_compression_level indicates the compression ratio. The default compression ratio is 6. The optional range is 1-9.-1 indicates disabling php zlib (gzip) compression.

2. Save the php. ini configuration file and restart the apache server.

3. Open the apache configuration file httpd. conf and configure the deflate_module to be loaded.

This step is the most critical step to enable the php gzip compressed output configuration. Many users will say that I have enabled php. the php gzip configuration in the ini configuration file still does not implement php gzip compression, because apache is not loaded with deflate_module. The method is as follows:

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

Remove the starting # and restart apache.


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.