search
HomeBackend DevelopmentPHP TutorialDetailed explanation of how PHP uses zlib extension to implement GZIP compressed output

This article mainly introduces the method of PHP using zlib extension to achieve GZIP compression output. It analyzes in detail the related operation skills of PHP gzip configuration and compression output in the form of examples. Friends in need can refer to this article

The example describes how PHP uses the zlib extension to implement GZIP compressed output. Share it with everyone for your reference, the details are as follows:

Generally, when we have a large amount of data transmission and hope to reduce the bandwidth pressure on the server, we will adopt a method to compress the file transmission. Using zlib in PHP can also implement gzip Compressed output, let’s look at a summary of various methods of GZIP compressed output.

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 in Windows environment there is no need to install zlib.

2. Install and build the php running environment

Since it is not enough to enable gzip configuration through the php.ini configuration file to achieve php gzip compression output, it requires the support of apache, so it is recommended to install and build php apache mysql operating environment.

php gzip configuration steps

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

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

function 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; //返回压缩的内容

After the 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 area, 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(&#39;ob_gzip&#39;);
//输出内容
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 of swf files Code

Example of file:

//没有加入判断swf文件是否已经压缩,入需要可以根据文件的第一个字节是&#39;F&#39;或者&#39;C&#39;来判断
压缩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);
//----------------------------------------------------------------------------------------------------
?>

Unzip 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 whole-site compression output, but uses the ob_gzhandler function on pages that require compressed output. You can only choose one of the two, otherwise an error will be reported.

2, 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 server

3. Open the apache configuration file httpd.conf, configure and load deflate_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 with deflate_module. The method is as follows, change

#LoadModule deflate_module modules/mod_deflate.so

Remove the # sign at the beginning and restart apache.

Articles you may be interested in:

Detailed explanation of how PHP implements distributed memcache to set up web cluster session synchronization

Example explanation of large file cutting and merging function implemented by PHP

Example explanation of simple word grouping algorithm implemented by PHP

The above is the detailed content of Detailed explanation of how PHP uses zlib extension to implement GZIP compressed output. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools