Coding Tips & Tricks


Home > WebP

WebP - Advanced image optimization using ASP.NET MVC

Most modern web pages contain a lot of images. These images often make up the bulk of a web page request. According to the HTTPArchive, images make up over 50% of the average web page and if you are looking to optimize on the performance of your web page, the best place to start is often with the images on your page. By optimizing your images and simply reducing the amount of bytes that your users are downloading, you can drastically improve your page load times.

There are some great tools out there that will allow you to optimize and compress your images, but you can only optimize your images to a certain extent before they start to lose their quality. It's about time we start looking at newer formats of images in order to decrease their file size, yet still maintain the image quality. This is where the WebP format comes in. It is pronounced as 'weppy' and is a new image format that was developed by the team at Google.

WebP Logo

WebP images are 26% smaller in size compared to PNGs and are 25-34% smaller in size compared to JPEG images. That is a massive saving! Google use WebP images instead of PNG images in Picasa, Gmail and its Chrome Web Store. Given the number of requests Chrome Web Store serves, this adds up to several terabytes of savings every day! As a web developer, you can use the WebP image format to create smaller and richer images that can help make the web faster. In order to compare the differences in image size, let's compare the following PNG below:

Visual Studio Logo

The image above is a PNG image that has a file size of 12KB. If I take this same image and convert it to WebP, it comes in at 4KB - that's almost a third of the size. Imagine being able to make that saving on each image in your website without compromising on quality!

While WebP images are awesome, unfortunately they are not without their limitations. WebP images are only currently supported in Google Chrome, Opera, Android, and Google Chrome Frame plugin for Internet Explorer. I still think that we should provide our users with the best and fastest browsing experience possible, so why not still offer this option to users that are capable of viewing WebP images?

In this article, I am going to run through an example that will show you how to create a simple HTML helper that you can use in your ASP.NET MVC applications to serve WebP images to browsers that are capable of handling them. In the case when the browser isn't capable of serving the images, it will simply fall back to its default image type. This simple change will reduce the size of the images on your page and improve your load times.

In order to get started with WebP images, you first need to download the encoder from the Google developer’s website.

The encoder allows you to easily encode most image formats into their WebP equivalent using a command line tool. Once you have downloaded the pack, it contains a set of tools that will allow you to encode and decode images into WebP format. The pack also contains a decent readme file that will get you up and running. The pack contains a tool called cwebp.exe which accepts the following syntax:

cwebp input.png -o output.webp

Once you fire up the command line and run the tool, pass it the original image and the name you would like it to output the WebP image to. It will produce a result similar to the following image:

WebP Encoder

Now that our image that has been converted into a WebP image, we can build a helper that will determine whether or not to display a WebP image or the original PNG image. I am going to steal/re-use some code that I wrote about in a previous article.

First up, we need to write a method that will detect if the browser is capable of handling WebP images.

The above method will detect if the current browser is Chrome, Opera, or Android. If true, it will return a Boolean notifying us that we can return the WebP image instead. Next, we need to create an HTML helper method that will return the path of the WebP format image depending on the browser capabilities.

The method above will detect if the browser is capable of handling WebP images and then replace the extension on the file with the new ".webp" extension. In order to use this Html helper, you simply need to call it in your view like so:

@Html.DrawImage("/content/images/visualstudio.png","Visual Studio")

We are almost there. If you hit F5 and fire up your application, you will see something similar to the following image:

WebP Mime Type Error

The image isn't quite showing up correctly, and if you navigate directly to the link in Chrome, you will receive a HTTP 404.3 error. "HTTP Error 404.3 - Not Found The page you are requesting cannot be served because of the extension configuration".

HTTP 403.3 Error

This is happening because the browser doesn't know how to interpret the WebP image because the mime type hasn't been sent through with it from the server. In order to rectify this, we simply need to add the MIME type to the Web.Config file.

When the MIME mapping has been added to your Web.Config, IIS will send this MIME type back with the WebP image request and the browser will understand how to display the image. After adding the config settings, fire up the application and tadaaaa!

Visual Studio Logo

If you use this HTML helper in your application, it will automatically determine whether or not to display the WebP equivalent image to your users or not. You will need to keep a copy of both the WebP image and the original image (PNG, JPG, and GIF) in order for this example to work. WebP images are still in their infancy, but I would like to see them being adopted by more and more websites. They are a great way to serve quality images to your users without compromising on quality.

If you would like to download a copy of the source code used in this example, it is available via the following link.








Comments

Andrew Herrick - 4/3/2013
Cool! I would just remove the first 'return' statement as the final one would catch all.

Andy Cohen - 4/8/2013
While this is interesting, I might not want to add this extra overhead to the server side rendering. I might play around with using Moderizr instead to detect browser capabilities and then inject a shim to serve up the Webp images instead.

Dean Hume - 6/9/2013
Hi Andy You raise a very interesting point! Detection can be done on the client or the server - it really depends on your needs. Check out this great article that compares the pros and cons of each - http://www.stucox.com/blog/client-side-vs-server-side-detection-for-webp/ Cheers Dean

mohammadreza berneti - 12/21/2013
u can just use css code : .mybackgroundimage { background-image: url("image.jpg"); background-image: image("image.webp" format('webp'), "image.jpg"); }

Anandi - 1/21/2014
These are much more advanced tips though, so many thanks for putting the guide together, seems I still have much to learn! http://goresult.in/

Jarrod Mosen - 7/22/2014
Rather than checking whether the browser is Chrome/Opera/Android, instead you should check the "Accepts" HTTP header for "webp" - some versions of these browsers do not support WebP.


Add your comment

300 Characters left


Please fill this in to confirm that you are human