banner image 1 banner image 2

Detect WebP browser support

April 11, 2022
3 mins
command
blog-img 1
Amar Balu
Author

That what, why, and how of WebP

By Amar Balu, a multi-sport champion!


WebP is a modern image format introduced by Google provides superior lossless and lossy compression for images on the web.

Why WebP ?

Webp uses VP8 to provide files of reduced size with quality equivalent to JPEG/PNG formats.

Due to the smaller file sizes, pages with WebP images load faster (better initial loading time) that will boost our site performance.

What is WebP and why do we need it?
Source : Illustration by OrangeCrush

And also WebP incorporates both transparency and animation at same time. Previously we used different formats, PNG for transparent backgrounds and GIF for animations but not provided with the same format.

How much smaller ?

WebP Images are 26% smaller in size compared to PNGs and 25–34% smaller than comparable JPEG formatted images with equivalent SSIM quality index.

Downsides of WebP

Despite of their benefits, not all browsers supports WebP. This post guides you how to use few strategies to determine WebP browser compatibility across client and server side environments.

1. CanIUse Tool

An online tool provides browser support tables for modern web technologies. Tool provides a clear picture of browser compatibility based on their versions.

[embed]https://caniuse.com/?search=webp[/embed]

Browsers are tagged with green label are compatible to WebP.

Source : https://caniuse.com/?search=Webp

2. Canvas

Canvas provides optimal solution to determine the compatibility of WebP across browsers with their .toDataUrl() method. This method takes to execute about 6ms, acts as an ideal solution for client side environments.

HTMLCanvasElement.toDataURL() method returns a data URI containing a representation of the image in the format specified by the type parameter.

Browsers take image/webp returns data:image/webp are compatible to WebP.

https://gist.github.com/amarbalu/fbc88887bec10bf816242f5c3cd2c061#file-canusewebp

3. Lossless Feature (Img element)

Method available in Google’s official document, provides attempt to decode a very small WebP image, to load an image element with webP Image and validate for success.

https://gist.github.com/amarbalu/32fbeca1410642aada04311f42a3c5e4#file-checkwebpfeature

Browser tries to load (onload) image element <img> without any error are compatible to WebP. Those browsers are non compatible will fall under error condition (onerror).

As the loading of images are non-blocking and asynchronous. Code depends on WebP support should preferably used in the callback function.

4. CSS

A simple Css rule that uses background-image’s url property, provides a simple check to detect WebP compatibility across browsers.

Browsers compatible to WebP, loads img/banner.webp image as background.

https://gist.github.com/amarbalu/8f90f65c4d199c087650ec46c1ea857f#file-checkwebpsupportcss

5. Headers

Web clients sends an accept request header, is a string provides the file formats that will be acceptable by the response.Act as ideal solution for server side environments.

Browser indicates image/webp under accept headers are compatible to WebP.

https://gist.github.com/amarbalu/822c4a3c3124a793e5b2f031fc26b398#file-checkwebpsupportssr

References


[embed]https://caniuse.com/?search=webp[/embed]

Meet the team!

Author
Amar Balu

Editor
Mridula Saravanan


We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com!
blog-img 2

Discussions

blog-img 3
5 mins
May 17, 2023
Sharing Data Between Controllers: Best Practices S...

This article will help you to understand the diffe

By Naveen C

blog-img 3
5 mins
March 21, 2023
Understanding Auto Layout and Constraints in Swift...

This article gives you an easy way of understandin

By Ramasamy P