Friday, September 21, 2012

G-zip compression in ASP.NET and php

Most of you may not know about g-zip compression and its benefits. G-ZIP compression can help bring down web page size including your CSS/ Javascript to 5 to 50%. Most of the time you will see your page size going down to nearly 15%.

Why should I use it?

  1. You will save bandwidth and a lot of server bandwidth as the size of the content responded is very small. On an average you will save about 80% of your bandwidth.
  2. You will bring down your load times by 80-90%. That is one thing that matters. Having a low load time is highly desirable and your users will love that your site is fast.


When should I not use it?

  1. If you have limited CPU resources. Compression takes some CPU resources so if you have fairly limited resources, compression can be taxing. This is rare though. The CPU or the server needs to be very old for this case.
  2. If you server very large dynamic web pages above 400KB. Compressing large dynamic content can be expensive.

How do I do it in php?

Include this is all your php pages.

<?php

// Include this function on your pages

function gzipped_page() {

    global 
$HTTP_ACCEPT_ENCODING;
    if( 
headers_sent() ){
        
$encoding false;
    }elseif( 
strpos($HTTP_ACCEPT_ENCODING'x-gzip') !== false ){
        
$encoding 'x-gzip';
    }elseif( 
strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){
        
$encoding 'gzip';
    }else{
        
$encoding false;
    }

    if( 
$encoding ){
        
$contents ob_get_contents();
        
ob_end_clean();
        
header('Content-Encoding: '.$encoding);
        print(
"\x1f\x8b\x08\x00\x00\x00\x00\x00");
        
$size strlen($contents);
        
$contents gzcompress($contents9);
        
$contents substr($contents0$size);
        print(
$contents);
        exit();
    }else{
        
ob_end_flush();
        exit();
    }
}

// At the beginning of each page call these two functions

ob_start();ob_implicit_flush(0);

// Do your main stuff here

echo 'I love compression';

// Call this function to output everything as gzipped content.

gzipped_page();?>

How do I do it in ASP.NET?

Paste this in your web.config, tested for ASP.NET 4.0, 3.5 and should work for 2.0.

<system.webServer>
    <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
      <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
      <dynamicTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
      </dynamicTypes>
      <staticTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
      </staticTypes>
    </httpCompression>
    <urlCompression doStaticCompression="true" doDynamicCompression="true"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

Do not worry about compatibility. G-ZIP is supported by all modern browsers.

Here is a list of supported browsers.

  • IE version 4 and above
  • Netscape Navigator version 4.06 and above 
  • Mozilla support since at least 1998 
  • FireFox from the very first version. 
  • Opera version 5.12 and above.
  • Safari had reports of buggy support only in early versions (2003) 
  • Chrome from the very first version
  • JAWS support began around 2005 
  • K-Meleon support since at least 2002, version 0.6 
  • Lynx support since version 2.6 
  • Camino support at least since version 0.6 
  • Sunday, May 20, 2012

    Metro style slideshow using jQuery, CSS and HTML!

    Type: Tutorial, Level:Easy

    I had a client who wanted a metro-style slide show on their homepage. I didn't knew much about metro, so after a few hit and trials, I finally managed to get it working. Here, is the final result with fiddle.  (Click on result tab below)



    Now, I will explain, how it works and the code.

    Wednesday, April 25, 2012

    Use HubTiles in Web

    The users of Windows Phone 7 must be familiar with HubTiles but those who are not, let me tell it is one of the best looking Silverlight Control out there!


    Wednesday, January 25, 2012

    The Rant - About Windows Phone 7


    This is the post that I have been meaning to write for a very long time now but I couldn't write because I was just so busy this past month. I dedicate this post to the most dedicated haters of Microsoft and Windows Phone 7 who cannot see the real picture and are blowing up their money on stupid Android phones and bloated iOS devices.

    Thursday, January 19, 2012

    Six of the most stupid comments on Windows Phone 7.5 devices/OS

    I'm absolutely tired of explaining to people that Windows Phone 7.5 is an extremely great mobile operating system. All this so called 'fanboyism' makes people so unreasonable! All of the major mobile phone operating systems might have their pros and cons but calling an OS bad/stupid/bloated just because you like the other one is plain stupid! I am also a fan of Microsoft but I don't go around calling every other brand stupid for no reason!

    HTC Radar


    Wednesday, July 20, 2011

    Why the iPhone isn't costly in India?

    Apple iPhone costs more than Rs. 30,000 in India so, many people think that the Apple iPhone is cheap in the US market since it costs only USD 200 which is approximately Rs. 10,000. Well, if you're one of them you're probably in the dark. Read on and get enlightened.


    Tuesday, June 21, 2011

    New Revamped Google Image Search

    Google has recently announced a new feature for its image search. It is being named “Google Search by Image”. Users can now use an image to search the web instead of just using the keywords. Instead of the conventional way of searching through keywords, images will now be used as your search query and the search results will render images similar to your image. If the image being searched is not found or is not as popular as it should be, Google will search for images, similar to the one being searched and display the result.


    The algorithm being used here is same as the one used for Google Goggles for mobile. It works like this: First, you submit the image to Google in any of the many ways available (including drag and drop and the regular upload). Now Google’s Image search engine will try and find images related to the image you input.
    While testing this feature, we found out that it worked really well without even a single image being identified incorrectly. The search results, as always were very fast.

    Let us know in the comments below how did you find the Google’s new image search feature.