PHP uses zlib extension to implement gzip compression output method

Source: Internet
Author: User
Tags fread
This article mainly introduces PHP using zlib extension to achieve gzip compression output method, now share to everyone, the need for friends can refer to

This example describes how PHP uses the zlib extension to implement gzip-compressed output. Share to everyone for your reference, as follows:

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 to implement 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 = Onzlib.output_compression_level = 6


Example 1

PHP uses zlib extensions for page gzip compression output

Code


function Ob_gzip ($content)//$content is the page content to compress {if (!headers_sent () && extension_loaded ("zlib") && Strstr ($_server["http_accept_encoding"], "gzip")//Determine whether the page header information is output, whether the zlib extension in PHP is 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


Copy the Code code as follows:

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


Copy the Code code as follows:

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

<?php//calls a function named Ob_gzip content to compress ob_start (' ob_gzip ');//output content Ob_end_flush ();//This is Ob_gzip function Ob_gzip ($content {if (!headers_sent () &&extension_loaded ("Zlib") &&strstr ($_server["http_accept_encoding"], "gzip" ) {$content = Gzencode ($content. "n//This page has been 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:


Not added to determine whether the SWF file has been compressed, into the need can be based on the first byte of the file is ' F ' or ' C ' to determine the compressed SWF file://----------------------------------------------------- ---------------------------------------------//FileName $filename = "test.swf";//Open File $rs = fopen ($filename, "R");// Read file Data $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 the file, use the highest compression level 9$body = Gzcompress ($body, 9);//merge header and content $str = $head. $body;//Close the Read file stream fclose ($rs);// Create a new file $ws = fopen ("create.swf", "w");//write File fwrite ($ws, $STR);//close file to leave fclose ($WS);//------------------------------- ---------------------------------------------------------------------? >


To extract the SWF file:


----------------------------------------------------------------------------------------------------//File name $ filename = "test.swf";//Open File $rs = fopen ($filename, "R");//Read file Data $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 the File $body = gzuncompress ($body);// Merge header and content $str = $head. $body;//Close the Read file stream fclose ($rs);//Create a new file $ws = fopen ("create.swf", "w");//write File fwrite ($ws, $STR);// Close file Stay fclose ($WS);//------------------------------------------------------------------------------------------- ---------? >


Example 3

Turn on PHP zlib (gzip) compression output

PHP gzip Configuration Knowledge Points:

1, the default PHP is not to turn on the Zlib entire station compression output, but by the need to compress the output of the page use ob_gzhandler function implementation, 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,


#LoadModule Deflate_module modules/mod_deflate.so


Remove the # number from the beginning 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.