WordPress Zlib Compression Functions in PHP

by admin on January 26, 2012 · 0 comments

WordPress Zlib Compression Functions in PHP

WordPress Zlib Compression Functions in PHPthumbnail

Instructions:

What is zlib?

The zlib compression format was created by the creators of zip and gzip compression to enable fast compression and decompression across platforms and software. Initially created as lossless image compression, zlib is commonly used for output compression, or compacting Web pages on demand as they are delivered to a browser, which in turn decompresses the page for viewing. This entire process is invisible to the user.

PHP and zlib

PHP offers zlib support on all platforms. The Windows version of PHP activates the zlib module by default, but an administrator must activate the module for other platforms. Once zlib is active, developers can make use of its output compression functions within their PHP code. The Apache Web server also includes a zlib module for output compression, and if the PHP module is not active, a Web developer can tune Apache’s zlib functionality via the site’s .htaccess file.

WordPress and zlib

WordPress itself does not make use of zlib, normally counting on Apache or PHP to compress its output. However, if zlib is enabled in PHP but not configured to compress output by default, a WordPress administrator can add the following line to the site’s index.php file to enable zlib compression:

ob_start(‘ob_gzhandler’);

Some WordPress plugins also enable zlib compression if it’s detected, but use alternate methods of compression if zlib is not available.

Coding zlib Functions in PHP

The PHP documentation includes detailed functions and code examples for using zlib within PHP code, including compressing and decompressing specific items on the server or obtaining information from compressed files. Elements such as images and style sheets can be compressed using zlib functions. To determine whether the PHP zlib module is active, create a text file named test.php and paste in the following line:

<?php phpinfo(); ?>

Upload the file to the Web server and then navigate using a Web browser to the file’s location. Look for the zlib library on the resulting page. If it doesn’t appear, contact your Web hosting provider or use Apache’s zlib functionality.

Previous post:

Next post: