Home >Backend Development >PHP Tutorial >PHP uses zlib extension to implement GZIP compression output step analysis
This time I will bring you an analysis of the steps for PHP to use the zlib extension to implement GZIP compression output. What are the precautions for PHP to use the zlib extension to implement GZIP compression output? The following is a practical case, let's take a look.
Under normal circumstances, when we have a large amount of data transmission and want to reduce the bandwidth pressure on the server, we will adopt a method to compress the file transmission. Using zlib in PHP can also achieve gzip compression output. Let's look at the GZIP compression output. Summary of various methods.
GZIP (GNU-ZIP) is a compression technology. After GZIP compression, the page size can be reduced to 30% or even smaller than the original size. In this way, users will feel refreshed and happy when browsing!
Preparation
1. Can’t find the php_zlib.dll file?
zlib compression has been built into php since php4.3, so at least Windows environment does not need to install zlib.
2. Install and build the php operating environment
Since it is not enough to enable php gzip compression output through php.iniconfiguration file, it requires the support of apache , so it is recommended to install and build the php apache mysql operating environment.
php gzip configuration steps
1. Open the php.ini configuration file and find zlib.output_compression = Off, Modify
zlib.output_compression = Off ;zlib.output_compression_level = -1
to
zlib.output_compression = On zlib.output_compression_level = 6
Example 1
PHP uses zlib extension to implement page GZIP compression output
Code
After thefunction ob_gzip($content) // $content 就是要压缩的页面内容 { if(!headers_sent() && extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip"))//判断页面头部信息是否输出,PHP中zlib扩 展是否已经加载,浏览器是否支持GZIP技术 { $content = gzencode($content." n//此页已压缩",9); //为准备压缩的内容贴上"//此页已压缩"的注释标签,然后用zlib提供的gzencode()函数执行级别为9的压缩,这个参数值范围是0-9,0 表示无压缩,9表示最大压缩,当然压缩程度越高越费CPU。 //用header()函数给浏览器发送一些头部信息,告诉浏览器这个页面已经用GZIP压缩过了! header("Content-Encoding: gzip"); header("Vary: Accept-Encoding"); header("Content-Length: ".strlen($content)); } return $content; //返回压缩的内容
function is written, call it with ob_start, so the original ob_start()
becomes
Copy code The code is as follows:
ob_start('ob_gzip'); //Add a parameter to ob_start(), and the parameter name is the function name just now. In this way, when the content enters the buffer, PHP will call the ob_gzip function to compress it.
Finally end buffer
Copy code The code is as follows:
ob_end_flush(); //End buffer, output content. Of course, you don't need this function, because the buffer content will be automatically output at the end of the program execution.
Final complete example
<?php //调用一个函数名为ob_gzip的内容进行压缩 ob_start('ob_gzip'); //输出内容 ob_end_flush(); //这是ob_gzip函数 function ob_gzip($content) { if(!headers_sent()&&extension_loaded("zlib") &&strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip")) { $content = gzencode($content." n//此页已压缩",9); header("Content-Encoding: gzip"); header("Vary: Accept-Encoding"); header("Content-Length: ".strlen($content)); } return $content; } ?>
Example 2
zlib compression and decompression swf file code
File example:
//没有加入判断swf文件是否已经压缩,入需要可以根据文件的第一个字节是'F'或者'C'来判断 压缩swf文件: //-------------------------------------------------------------------------------------------------- //文件名 $filename = "test.swf"; //打开文件 $rs = fopen($filename,"r"); //读取文件的数据 $str = fread($rs,filesize($filename)); //设置swf头文件 $head = substr($str,1,8); $head = "C".$head; //获取swf文件内容 $body = substr($str,8); //压缩文件内容,使用最高压缩级别9 $body = gzcompress($body, 9); //合并文件头和内容 $str = $head.$body; //关闭读取的文件流 fclose($rs); //创建一个新的文件 $ws = fopen("create.swf","w"); //写文件 fwrite($ws,$str); //关闭文件留 fclose($ws); //---------------------------------------------------------------------------------------------------- ?>
Decompress the swf file:
//---------------------------------------------------------------------------------------------------- //文件名 $filename = "test.swf"; //打开文件 $rs = fopen($filename,"r"); //读取文件的数据 $str = fread($rs,filesize($filename)); //设置swf头文件 $head = substr($str,1,8); $head = "F".$head; //获取swf文件内容 $body = substr($str,8); //解压缩文件内容 $body = gzuncompress($body); //合并文件头和内容 $str = $head.$body; //关闭读取的文件流 fclose($rs); //创建一个新的文件 $ws = fopen("create.swf","w"); //写文件 fwrite($ws,$str); //关闭文件留 fclose($ws); //---------------------------------------------------------------------------------------------------- ?>
Example 3
Enable php zlib (gzip) compression output
php gzip configuration knowledge points:
1. By default, PHP does not enable zlib compression output for the entire site. Instead, it uses <a href="http://www.php.cn/wiki/584.html" target="_blank">ob_gzhandler</a>## for pages that require compressed output. #Function implementation, you can only choose one of the two, otherwise an error will be reported.
zlib.output_compressionThe 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 compression ratio is 6. The optional range is 1-9. -1 represents turning off php zlib (gzip) compression
2. Save the php.ini configuration file , and restart the apache server3. Open the apache configuration file httpd.conf, configure and loaddeflate_module
This step is the most critical step to enable php gzip compression output configuration. Many netizens will say that even though I have enabled the php gzip configuration in the php.ini configuration file, I still don’t realize php gzip compression. This is because apache is not loaded withdeflate_module. The method is as follows, replace
#LoadModule deflate_module modules/mod_deflate.soI believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
Detailed explanation of the steps for cutting and merging large files in PHP
Detailed explanation of the steps for PHP mongoDB database operation
The above is the detailed content of PHP uses zlib extension to implement GZIP compression output step analysis. For more information, please follow other related articles on the PHP Chinese website!