PHP uses zlib extension to realize GZIP compression output

  • 2021-09-20 19:42:31
  • OfStack

In this paper, an example of PHP using zlib extension to achieve GZIP compression output method. Share it for your reference, as follows:

Under normal circumstances, we have a large number of data transmission reasons, hoping to reduce the bandwidth pressure of the server, and will adopt one way to compress file transmission. zlib can also realize gzip compression output in php. Let's look at the summary of various methods of GZIP compression output below.

GZIP (GNU-ZIP) is a compression technology. After GZIP compression, the page size can be reduced to 30% or less. In this way, users will feel very cool and happy when browsing!

Preparatory work

1. The php_zlib. dll file cannot be found?

zlib compression has been built into php since php 4.3, so at least Windows does not need to install zlib.

2. Install and build php running environment

Since it is impossible to realize php gzip compression output by opening gzip configuration through php. ini configuration file, it needs the support of apache, so it is recommended to install and build php+apache+mysql running environment.

php gzip Configuration Steps

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


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

Modify to


zlib.output_compression = On
zlib.output_compression_level = 6

Example 1

PHP uses zlib extension to realize page GZIP compression output

Code


function ob_gzip($content) // $content  Is the content of the page to be compressed 
{
if(!headers_sent() && extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip"))// Judge whether that page header information is output, PHP Medium zlib Expansion   Whether the exhibition has been loaded and whether the browser supports it GZIP Technology 
{
$content = gzencode($content." n// This page is compressed ",9); // Paste the content to be compressed "// This page is compressed " Comment label of, and then use the zlib Offered gzencode() Function execution level is 9 The range of this parameter value is 0-9 , 0  Indicates no compression, 9 Indicates the maximum compression, of course, the higher the compression degree, the more expensive it is CPU . 
// Use header() Function to send to the browser 1 Some header information to tell the browser that this page has been used GZIP It's compressed! 
header("Content-Encoding: gzip");
header("Vary: Accept-Encoding");
header("Content-Length: ".strlen($content));
}
return $content; // Returns compressed content 

After the function is written, it is called with ob_start, so the original ob_start() Become

ob_start('ob_gzip'); // To ob_start() Plus 1 The parameter name is the function name just now. So that when the content enters the buffer, PHP Will call the ob_gzip Function compresses it. 

Last end buffer

ob_end_flush(); // End the buffer and output the contents. Of course, it's OK not to use this function, because the program will automatically output the buffer contents at the end of execution. 

The final complete example


<?php
// Call 1 Function named ob_gzip Compress the contents of 
ob_start('ob_gzip');
// Output content 
ob_end_flush();
// This is 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

Code for zlib Compression and Decompression of swf Files

Examples of files:


// No judgment was added swf Is the file compressed , You can enter according to the section of the document 1 Bytes are 'F' Or 'C' To judge 
 Compression swf Documents :
//--------------------------------------------------------------------------------------------------
// Filename 
$filename = "test.swf";
// Open a file 
$rs = fopen($filename,"r");
// Read the data of the file 
$str = fread($rs,filesize($filename));
// Settings swf Header file 
$head = substr($str,1,8);
$head = "C".$head;
// Get swf File content 
$body = substr($str,8);
// Compressed file contents , Use the highest compression level 9
$body = gzcompress($body, 9);
// Merge file headers and contents 
$str = $head.$body;
// Close the read file stream 
fclose($rs);
// Create 1 A new file 
$ws = fopen("create.swf","w");
// Write a file 
fwrite($ws,$str);
// Close the file and leave it 
fclose($ws);
//----------------------------------------------------------------------------------------------------
?>

Extract the swf file:


//----------------------------------------------------------------------------------------------------
// Filename 
$filename = "test.swf";
// Open a file 
$rs = fopen($filename,"r");
// Read the data of the file 
$str = fread($rs,filesize($filename));
// Settings swf Header file 
$head = substr($str,1,8);
$head = "F".$head;
// Get swf File content 
$body = substr($str,8);
// Unzip file contents 
$body = gzuncompress($body);
// Merge file headers and contents 
$str = $head.$body;
// Close the read file stream 
fclose($rs);
// Create 1 A new file 
$ws = fopen("create.swf","w");
// Write a file 
fwrite($ws,$str);
// Close the file and leave it 
fclose($ws);
//----------------------------------------------------------------------------------------------------
?>

Example 3

Turn on the php zlib (gzip) compression output

php gzip Configuration Knowledge Points:

1. By default, php does not open the whole station compression output of zlib, but uses the ob_gzhandler Function implementation, the two can only choose 1 from 2, otherwise an error will be reported.

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

3. zlib.output_compression_level stands for compression ratio. The default recommended compression ratio is 6, and the optional range is 1-9.-1 stands for turning off 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 to load deflate_module

This step is the most critical step to turn on php gzip compression output configuration. Many netizens will say that I have turned on php. How can php gzip configuration in ini configuration file still not realize php gzip compression because apache is not loaded with deflate_module? The method is as follows


#LoadModule deflate_module modules/mod_deflate.so

Remove the # at the beginning and restart apache.

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Cache Technology", "Summary of PHP Network Programming Skills", "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: