Cloudinary Blog

Check for WebP Browser Support to Dynamically Deliver Images

Check for WebP Browser Support to Dynamically Deliver Images
Modern image compression techniques have had a large impact on our lifestyle. Digital cameras can save thousands of high-quality photos on a single memory card, smartphones can quickly share high resolution images on-the-fly, and websites and mobile apps can show rich media quickly. All of this just couldn,t have worked if image data was stored at its original, raw form. 
 
Through our apps, browsers and devices, we all use the JPEG lossy format to manage photos efficiently and the PNG lossless format to deliver graphics, icons and drawings.
 
However, while video formats have gone a long way these past few years, image formats have stayed somewhat behind. 
 
This changed at 2010 when Google first introduced the new WebP (pronounced "Weppy") format, an open sourced image format that supports both lossy and lossless image compressions.
 
page load speed
 
WebP generates files significantly smaller than the popular JPEG and PNG formats. WebP also supports many advanced features, such as transparencies in its lossy mode, and more eye pleasing degradation in image quality on its lower quality, higher compression settings.
 
Smaller files mean less bandwidth consumption and reduced traffic costs for your online services and better user experience with faster delivery & image sharing times for users of your web sites and mobile apps.
 
In this blog post we wanted to introduce Cloudinary's new cloud-based support for on-the-fly generation of WebP images.
 

Generating smaller images of equal (or better) quality

If you are already familiar with Cloudinary, you probably know that you can tell Cloudinary to do image format conversions on-the-fly simply by changing the file extension of your CDN delivered URLs.
 
Consider for example, a JPG image that was uploaded to the cloud with the public ID 'sample'. Now we want to embed a 300x200px thumbnail of this image on our website. 
 

Webp browser support 

 
The left image below is generated by Cloudinary using the JPG format while the right image is generated using the WebP format. Note that you need a Google Chrome browser to view the WebP image correctly.
 
 
   
                           27.9KB JPG                                                              12.4KB WebP
  
If you check the network stats, you will notice that the JPG file is 27.9 KB while the WebP file is just 12.4 KB in size and looks almost the same. This means that we've just saved 56% in bandwidth. If your website includes many images, you can save half of your traffic, get your site to load twice as fast and make your visitors happy.
 
If you use our client libraries, either concatenate '.webp' to the public ID or set the 'format' parameter to 'webp'. For example, in Ruby on Rails and PHP:
Copy to clipboard
<%= cl_image_tag("sample.webp", :crop => :scale, :width => 300) %>
Copy to clipboard
<?php echo cl_image_tag("sample", array("format" => "webp", "width" => 300, 
                        "crop" => "scale")); ?>

 

Lower image quality equals smaller file size

JPG and WebP are lossy formats, allowing you to control the quality of the target image. 
 
There's a clear tradeoff between the target image's quality and its generated file size. In many cases, you will be able to considerably reduce the quality and size of your images while your users will barely notice the difference. 
 
The JPEG format has noticeable artifacts in lower quality levels, especially if fine details or texts are involved. The WebP format has less noticeable artifacts but tends to generate a slightly blurry image in its lower quality settings.
 
You can test that yourself by setting Cloudinary's 'quality' parameter (or 'q' for URLs) to any value between 1 to 99 (the default is 90). 
 
The following example reduces the quality of the sample image to 30%. On the left there's a JPG of 30% quality and on the right a WebP of 30% quality.
 
 
     
                              10.6KB JPG                                                          7.1KB WebP
 
The sharp-eyed will notice the difference. On the other hand, the drastic quality reduction still resulted in an image that can be considered decent, especially when using the WebP format. 
 
The JPG of 30% quality is 10.6KB while the WebP of 30% quality is just 7.1KB in size.
 
In this comparison, the WebP format saved 33% in size, which is very nice. It's interesting to notice that this low quality WebP is just 25% the size of the original JPG while delivering a comparable result, quality-wise.
 
The following Node.js and Django examples generate delivery URLs similar to the example above:
Copy to clipboard
cloudinary.image("sample.webp", { width: 300, crop: 'scale', quality: 30 });
Copy to clipboard
cloudinary.CloudinaryImage("sample.webp").image(width = 300, 
                                                crop = 'scale', quality = 30)

 

Semi-transparent images

Recently, Facebook, Google+ and many other websites, have started to display user profile pictures with a completely circular crop. In order to display such circular images on a non-solid or changing background, the images need to be semi-transparent. The JPG format doesn't support transparent pixels, so you might be forced to use PNG which is far from optimal for this purpose.
 
The following example uses Cloudinary to generate a circular face-detection based thumbnail in the JPG format. The JPG format is effective for this image and takes just 13.6KB. However, as you can see, you cannot place it on a non-white background.
 
 
 
               
 
In order to make the edges transparent, we convert the image to the PNG format. As you can see below, it looks great. But the resulting image is 63.6KB in size which is almost x5 bigger than the JPG version. This will waste bandwidth & money and will harm the user experience.
 

 
The example below generates the same image with the WebP format. The image looks great, the edges are transparent and the file is just 9.8KB in size, which is even smaller than the non-transparent JPG version.
 

 

Lossless WebP images

All WebP examples above were using the lossy WebP format with a 90% quality, or lower. 
 
WebP also supports a lossless format similar to the PNG format. This lossless format is useful for rasterized vector graphics (e.g., logos), that might be visibly degraded in quality using the lossy format.
 
In order to generate a lossless WebP file, simply set Cloudinary's 'quality' parameter to 100:
 

 
Same example in Ruby on Rails:
Copy to clipboard
<%= cl_image_tag("sample.webp", :quality => 100) %>

 

Format adoption

As mentioned above, not all browsers support the WebP format yet. 
 
At present, WebP is supported by Google Chrome, Android 4.0+, and Opera. This is a serious disadvantage, as you'll need to selectively decide when to to utilize it.
 
On the other hand, Chrome is quite popular, especially if you have an application targeted on early adopters and techies. Even if you improve the user experience only for your Chrome users and save only the part of your bandwidth that is consumed by Chrome, it might still be worth the hassle of using WebP. 
 
For example, below you can see the statistics of Cloudinary's web site for the last 30 days. Chrome is responsible for 62% of our traffic.
 
 

Dynamic WebP generation based on visitor's browser

To support serving webp only to Chrome/Android4/Opera visitors, you can use Javascript code to lazy load images according to your visitors’ browsers.
 
Cloudinary's jQuery plugin allows you to dynamically generate delivery URLs that will manipulate your images on the fly and deliver the result through a fast CDN with advanced caching.
 
For example, you can define image tags in your HTML page by setting the 'src' attribute to a blank image and set the 'data-src' attribute to the public ID of the actual image uploaded to Cloudinary ('sample' in this example).
Copy to clipboard
<img src="blank.png" width="150" height="100" data-crop="fill" data-src="sample.jpg"/>
The following jQuery code will convert all image tags to dynamically generated image URLs instead of the placeholder blank image.
Copy to clipboard
$('img').webpify();
The page is loaded quickly without waiting for the images to load. Afterward, the jQuery code above will dynamically update the image tags to display either a WebP image or a JPG image, depending on whether there is WebP browser support or not.
 
Chrome result:
Copy to clipboard
<img src="https://res.cloudinary.com/demo/image/upload/c_fill,h_100,w_150/sample.webp"
         width="150" height="100" data-crop="fill" data-src="sample.jpg">
 
Other browsers result:
Copy to clipboard
<img src="https://res.cloudinary.com/demo/image/upload/c_fill,h_100,w_150/sample.jpg"
         width="150" height="100" data-crop="fill" data-src="sample.jpg">
See our documentation for more details about the jQuery plugin.
 

Summary

Web sites and apps are becoming more and more media-rich. It's important to keep track of our bandwidth utilization, optimize downloads and uploads time and improve our users' experience.
 
The WebP format is another great tool to achieve our goals. With Cloudinary's on-the-fly, cloud-based WebP image format conversion, this becomes much simpler.
 
The WebP format support is available for all our free and paid plans. Sign up now for a free account if you haven't done so already.
 

Recent Blog Posts

Maya Shavin: How I Built My Website

Besides working as a senior front-end developer at Cloudinary, I'm also a content creator, a blogger, and an open-source developer. Follow me at @mayashavin and on mayashavin.com.

In the beginning, my website, mayashavin.com, was mainly for showcasing the status of my development projects and keeping me organized with my speaking schedule. Initially, I built it with Vue.js, later on switching to Nuxt.js (aka Nuxt) for a higher SEO score, and deployed it with Netlify. After some time, I added a blog section with Netlify CMS as the content management system (CMS). Everything was fine until I added more content and features, which led to a significant decline in the site’s performance. Also, the site design needed a modern look. So, I gave the site a makeover.

Read more
Automation Frees Up PetRescue’s Staff to Help Pets Find Their Forever Homes

As we spend more time at home, many of us are adopting pets for the joy, companionship and a surprising range of health benefits. In Australia, where our nonprofit customer PetRescue is located, there’s a shortage of pets to adopt. Last August, the Guardian reported that dog shelters in Australia emptied and adoption fees for puppies were running as high as $AUS1800.

Read more
Cloudinary and Contentful Make Modern Content Management Easier

I am pleased to share that Cloudinary and Contentful have joined forces to further streamline the creation, processing, and delivery of online content through Cloudinary’s digital asset management (DAM) solution and advanced transformation and delivery capabilities for images and video. What’s more, the partnership delivers a headless approach to DAM. By leveraging APIs for media management tasks, marketers and developers alike benefit from an integrated stack of optimized assets for optimization and automation. As a result, page loads are fast and beautiful, and at scale—with less overhead and effort.

Read more
Introducing Cloudinary's Nuxt Module

Since its initial release in October 2016 by the Chopin brothers as a server-side framework that runs on top of Vue.js, Nuxt (aka Nuxt.js) has gained prominence in both intuitiveness and performance. The framework offers numerous built-in features based on a modular architecture, bringing ease and simplicity to web development. Not surprisingly, Nuxt.js has seen remarkable growth in adoption by the developer community along with accolades galore. At this writing, Nuxt has earned over 30K stars on GitHub and 96 active modules with over a million downloads per month. And the upward trend is ongoing.

Read more
How Quality and Quantity can go Hand in Hand

When it comes to quality versus quantity, you’ll often hear people say, “It’s the quality that counts, not the quantity”. While that’s true in many situations, there are also cases where you want both quality and quantity. You may have thousands of images on your website and you want them all to look great. This is especially important if your website allows users to upload their own content, for example, to sell their own products or services. You don't want their poor quality images to reflect badly on your brand.

Read more