Compresser son site avec Gzip

Compresser son site avec Gzip


Jan 21 2013, 18:18 in Web Development

Rendre son site web beaucoup plus rapide, c'est très simple!

Voici toutes les informations dont vous avez besoin, tirées directement du site http://www.whatsmyip.org/http-compression-test/phpgzip :

What?

Gzip. Its a compression algorithm. Similar to ZIP and SIT, gzip is very common. If you are dealing with uncompressed data, you can save tons of space by gzipping your files. An image file is already compressed using jpeg or gif algorithms, and so as is the nature of data, compressing it again with gzip won't give you much. But if you have raw text, or even better, xml or html files, you are in for a treat. Most people never think of it, because image files have always been so much larger than the HTML files themselves. But more and more, HTML files are growing and growing, with more complicated page layouts and things like JavaScript, 40K HTML files are common place and easily/often surpassed. Now when you think about what HTML is, most of the HTML file itself is HTML tags, the same tags used over and over again. This makes HTML one of the BEST things to compress. You can easily compress a 40K file down to 3K.

How?

But how is it implemented? Believe it or not, nearly all browsers support gzipped content. Its been built into them for years. Its just that no one has bothered to use it. There are 3 main ways to gzip your code. Two of them involve using mod_gzip. Now mod_gzip does work, but to be honest it is a bitch to set up. Once its configured, you can set it up one of two ways. Apache can either automatically gzip all html on its way out, on the fly. Or, you can store 2 versions of each page on your site, a gzipped version, and a regular version

For example, you'd have in your home directory:

index.html
index.gz

Browsers that supported gzip, would get the gz file. Ons that didn't would get the html file. Either way you set up mod_gzip, Apache will automatically send gzipped files to browsers that support it, and straight html to browsers that don't. And from the client, there is no change, no difference, the page sends, decompresses and loads exactly the same from the user point of view.

`By now you might be thinking, it would be a pain in the ass to have to maintain gzipped copies of every page on my site. Your right it would, you shouldn't do it that way. You might also be thinking that having apache gzip on the fly will bog down your server. But it won't. Heres the real beauty of gzip. Because the file is so much smaller, it takes significantly less time to transmit. And the web sever doesn't have to maintain a connection for nearly as long. As a result, there is no performance hit on the server whatsoever. Add that to the fact that you are going to take a sizable bite out of your bandwidth usage each month. And the fact that high speed users are going to load your site a bit faster, and dial up users are going to load your site SIGNIFICANTLY faster. using gzip seems like a no brainer, and it surely IS a no brainer (in my opinion anyway). Wheres the downside? Configuration. Setting it up is a pain in the ass. And if you are on a web host, most don't even use mod_gzip. If you are moving your site from one server to another, you'll have to set it up on the new one again, if possible.

So I bring you the 3rd way to do it. Its so easy, you'll think your doing something wrong. Forget mod_gzip. Use PHP instead. All you have to do is put the following line at the VERY top of each PHP document, and just like magic, the entire HTML of the page will be compressed!

Code:

<!--? ob_start("ob_gzhandler"); ?-->

This is so easy, just paste it at the beginning of your PHP and thats IT! It automatically gzips the page for browsers that support it. And its portable. Nearly every webhost uses PHP, so with no configuring, you can move your site from server to server, and every server that uses PHP, you'll automatically get compressed code. If you use PHP already, you are probably thinking 'oh my god where have you been all my life' but if you don't, your probably scratching your head wondering what php is. I can't go into too much detail here, but basically: If your server is running PHP, all you have to do is change your files from webpage.html to webpages.php. And now your file is a PHP file. You can still edit it no problem in programs like Adobe GoLive. And just slip the line of code at the absolute beginning of the file, and you'll clearly see the difference.

If you are wondering about where you should use this and where you shouldn't, I'll tell you where: Everywhere. Put it at the top of every single page on your site (or use an include( ) function in PHP). Every HTML page will get at least some compression. The bigger and more complicated the page is, the better it will compress.

Now, to reiterate what I've said. If you want to gzip all your HTML code to speed up your web server, speed up your page load times, and use significantly less bandwidth, while all at the same time doing nothing different to edit, maintain, or view your web sites, then simply slip that one line of PHP at the top of each document and you are good to go!

Wondering what a gzip page really looks like? Every page on www.whatsmyip.org uses php gzipping. I've been using it for years, and it works like a charm!

How to verify its working:

gzip works so seamlessly, no doubt you'll be wondering, "is it working?" Here's the easiest way to check. Just go to the GZip Tester on this site, and type in the address of your page. Our tool will scan your page and determine if it is sending out gzipped content or not.

You can also check the "manual" way. Load the page in FireFox. Now in the 'Tools' menu, choose 'Page Info'. Now in the window, you will see a size value. That is the actual size of the html of the page as it was transmitted over the internet. Now go to 'File' and save the page to your desktop. It will save the html file and probably a folder with all the images too. Just toss the images. Now Get Info on the html file, and you will see the size of the uncompressed code. If you are not using gzip, they will be nearly identical. If you ARE using gzip, the first value will be significantly smaller than the second. If you really want to be wowed, use that method to verify this page on my site:

http://www.whatsmyip.org/html-characters Then you will see the power of gzip!

En résumé, pour activer Gzip sur votre site, copiez simplement cette ligne php à la toute première ligne de votre fichier php:

ob_start("ob_gzhandler");

Pour tester si Gzip est activé : http://www.gidnetwork.com/tools/gzip-test.php


Share:
Like:

Disable AdBlock on this domain and offer me a cup of coffee :)