GZIP (Gnu-zip) is a compression technique. The page size can be turned into 30% or smaller after gzip compression. This way the user will feel very happy when browsing!
Preparatory work
1, can not find the Php_zlib.dll file?
Since the php4.3 began zlib compression has been built into PHP, so at least in the Windows environment is not required to install zlib.
2. Install and build PHP operating environment
Because the light through the php.ini configuration file to open the gzip configuration to achieve PHP gzip compression output is not good, 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, find Zlib.output_compression = off, will
Zlib.output_compression = Off
; zlib.output_compression_level =-1
Amended to
Zlib.output_compression = On
Zlib.output_compression_level = 6
Instance 1
PHP uses zlib extension to implement page gzip compression output
Code
The code is as follows |
Copy Code |
function Ob_gzip ($content)//$content is the content of the page 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 has been loaded, and whether the browser supports GZIP technology { $content = Gzencode ($content. "n//This page has been compressed", 9); Post a "//this page compressed" comment label for the content being compressed, and then perform a 9-level compression with the Gzencode () function provided by zlib, which ranges from 0-9,0 to zero compression, and 9 for maximum compression, which, of course, costs 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 contents of a compressed |
When 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 (), which is the name of the function you just called. So when the content goes into 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 also OK, because the program will automatically output the buffer content at the end of execution.
The final complete instance
The code is as follows |
Copy Code |
<?php Call a function named Ob_gzip content to compress 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 has been compressed", 9); Header ("Content-encoding:gzip"); Header ("vary:accept-encoding"); Header ("Content-length:". strlen ($content)); } return $content; } ?> |
Instance 2
Zlib compression and decompression of SWF file code
Examples of files:
The code is as follows |
Copy Code |
Not joined to determine whether the SWF file has been compressed, in need can be based on the first byte of the 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 file contents using the highest compression level 9 $body = Gzcompress ($body, 9); Merging file headers and content $str = $head. $body; Turn off the Read file stream Fclose ($RS); Create a new file $ws = fopen ("create.swf", "w"); Write a file Fwrite ($ws, $STR); Close file Stay Fclose ($WS); //---------------------------------------------------------------------------------------------------- ?> To extract 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 = "F". $head; Get SWF file contents $body = substr ($STR, 8); Extract the contents of a file $body = gzuncompress ($body); Merging file headers and content $str = $head. $body; Turn off the Read file stream Fclose ($RS); Create a new file $ws = fopen ("create.swf", "w"); Write a file Fwrite ($ws, $STR); Close file Stay Fclose ($WS); //---------------------------------------------------------------------------------------------------- ?> |
Instance 3
Turn on PHP zlib (gzip) compression output
PHP gzip Configuration Knowledge Point:
1, the default PHP is not open zlib whole 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, otherwise it will be an 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 compression ratio, the default recommended set compression ratio of 6, optional range for 1-9,-1 on behalf of the shutdown PHP zlib (gzip) compression
Second, save the php.ini configuration file, and restart the Apache server
Third, open the Apache configuration file httpd.conf, configure the load Deflate_module
This step is the most critical to open PHP gzip compression output configuration steps, many netizens will say I have opened the php.ini configuration file PHP gzip configuration how or not to implement PHP gzip compression, is because the Apache loaded Deflate_module, The following methods are used to
The code is as follows |
Copy Code |
#LoadModule Deflate_module modules/mod_deflate.so |
Remove the opening #, and restart Apache.