How to: Gzip compression with PHP

Gzip compression is a quick and easy way to decrease page size and lower bandwidth bills.

PHP is the scripting language of choice for web applications built at Kaifu Computing. We are obsessed with finding ways to increase the performance of our solutions - reducing a page load by 0.1 seconds makes us very happy.

Gzip is the name of a compression/decompression library released by the GNU Project under a free, open-source license. It is based on Lempel-Ziv coding, otherwise known as LZ77.

In PHP it only takes a single line of code to Gzip compress the generated output, which is usually HTML. Here it is:

<?php ob_start("ob_gzhandler"); ?>

That's it. Place it at the start of your application before any output is sent and PHP will automatically buffer any generated output, Gzip compress it and then send the appropriate headers and data to the visitor.

Here are some figures taken from kaifu.co.uk to demonstrate how effective this single line of code really is:

Page size without Gzip: 10.57 KB

Page size with Gzip on: 3.15 KB

This is a compression ratio of 70.2%! This saving is not a hypothetical best case, it is a real world saving available to most website owners. Reducing the page size by 70% will reduce your bandwidth costs by 70% - a serious saving for even a small website.

Talk to Kaifu Computing today to get your website optimised to load quickly for anyone, anywhere in the world.

Kaifu Computing help companies gain a competitive advantage. Talk to one of our fantastic advisors today. The advice is free and we promise a friendly service aimed at meeting, and exceeding, your needs.


Posted on Sunday 17 October 2010 to the category Blog |
Used tags: , , , ,

Share to your favourite website

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Del.icio.us
  • Digg
  • Google
  • Reddit
  • StumbleUpon
  • Facebook
  • Twitter

Comments

Not bad, my compression ratio was on average about 60% - not quite as good as yours.
D_Kiss - Sunday 17 October 2010 at 10:16am
how are you I was luck to seek your Topics in wordpress
your post is marvelous
I get much in your subject really thanks very much
btw the theme of you website is really terrific
where can find it
bet365 - Sunday 17 October 2010 at 10:16am
Good day!

This was a really excellent website!
I come from roma, I was fortunate to come cross your blog in google
Also I get much in your blog really thanks very much i will come again
thanks41 - Sunday 17 October 2010 at 10:16am
Good day!This was a really wonderful subject!
I come from milan, I was luck to come cross your blog in bing
Also I get much in your website really thank your very much i will come daily
bet365 - Sunday 17 October 2010 at 10:16am
What a great resource!
CNA Training - Sunday 17 October 2010 at 10:16am
If you could e-mail me with a few suggestions on just how you made your blog look this excellent, I would be grateful.
small business grants - Sunday 17 October 2010 at 10:16am
hello!This was a really quality subject!
I come from itlay, I was luck to seek your Topics in google
Also I obtain much in your theme really thank your very much i will come again
bet365 - Sunday 17 October 2010 at 10:16am
If you are open to having a guest blog poster please reply and let me know. I will provide you with unique content for your blog, thanks.
Vlad - Sunday 17 October 2010 at 10:16am
Keep posting stuff like this i really like it
Robert C - Sunday 17 October 2010 at 10:16am

One or more comments are waiting for approval by an editor.

Leave a comment