8.13. Compressing Web Output with gzip
Problem
You want to send compressed content to browsers that support automatic decompression.
Solution
Add this setting to your php.ini file:
zlib.output_compression=1
Discussion
Browsers tell the server that they can accept compressed responses
with the
Accept-Encoding
header. If
a browser sends Accept-Encoding: gzip or
Accept-Encoding:
deflate, and
PHP is built with the zlib extension, the
zlib.output_compression
configuration directive
tells PHP to compress the output with the appropriate algorithm
before sending it back to the browser. The browser uncompresses the
data before displaying it.
You can adjust the compression level with the
zlib.output_compression_level
configuration
directive:
; minimal compression zlib.output_compression_level=1 ; maximal compression zlib.output_compression_level=9
At higher compression levels, less data needs to be sent from the server to the browser, but more server CPU time must be used to compress the data.
See Also
Documentation on the zlib extension at http://www.php.net/zlib.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access