According to a W3Techs survey, the images on 74 percent of websites worldwide are in JPEG or PNG format and for good reason: those images display well on all browsers. However, several newer image formats are well worth consideration, a leading example being WebP. This post describes how to adopt WebP as your image format and, accordingly, lower your image weight by approximately 30 percent and reduce the load time of your websites or native apps.
WebP is an image format developed by Google, specifically for efficient loading of images online, presenting smaller yet more visually-pleasing pictures. Typically, WebP compresses images by an average of 30 percent more than JPEG with no loss in quality. WebP files can be saved with either lossless or lossy compression. Notably, WebP lossless images are 26% smaller than PNGs.
However, WebP works on the Chrome, Android, and Opera browsers only, which would deter wide adoption of the format. On the other hand, given the steady rise of Chrome usage over the past years—at 81 percent as of July 2019 according to a W3Schools survey—optimization of images for display on Chrome is worth pursuing.
As a matter of course, Android being an OS developed by Google, WebP works well on Android. Specifically:
- Lossy WebP images are supported in Android 4.0 (API level 14) and higher.
- Lossless and transparent WebP images are supported in Android 4.3 (API level 18) and higher.
To read and show WebP images in an Android project, download the webp library, create a “jni” folder in your Android project, copy libwep content to it, and in jni/Android.mk add necessary configurations. After successful compilation, you will obtain libwebp.jar, which should be included in your project build path
In addition, you can render WebP images on iOS through a library called libwebp, which encodes and decodes them. libwebp is available as precompiled binaries for iOS or as source code.
WebP supports the following image features:
- Lossy or lossless compression
- Transparency
- Extensible Metadata Platform (XMP) metadata
- International Color Consortium (ICC) profiles
- Animation
- Color Space * Lossy WebP works exclusively with the 8-bit YUV420 format. * Lossless WebP works exclusively with the RGBA [red green blue alpha] format.
See this example of how JPEG, PNG, and GIF images look versus their WebP counterparts.
JPEG -> WebP (Resolution: 300×190. Quality: 80)
PNG -> WebP (Resolution: 300 x 192. Image with transparency)
A GIF -> A WebP (Resolution: 300×168. Quality: 80. Animation)
One way to adopt WebP as your image format is by adding the HTML5 <picture> tag in which to define various image properties, as in this example:
<picture>
<source type="image/webp" srcset="images/cat.webp">
<img src="images/cat.jpg" alt="A cat">
</picture>
The browser then downloads the first image according to the specified source type, in this case WebP, assuming that it supports that format. For browsers that don’t recognize the <picture> tag, adopt WebP through a polyfill, such as Picturefill.
In the case of multiple images, rather than specifying a WebP variant for each of them, take advantage of Cloudinary’s automation capability (click here for a Cloudinary free-forever account), like this:
After uploading an image to Cloudinary, add to the image URL the parameter that directs Cloudinary to responsively display the image in the optimal format for the browser in question. If an image request originates from Chrome or Opera, Cloudinary converts its format to WebP on the first request, caching the image on the content delivery network (CDN) for subsequent requests.
In addition, Cloudinary responsively delivers the image in JPEG format to the Firefox browser and JPEG-XR format for the Internet Explorer browser.
For more details, see the related documentation.
For native mobile apps on Android, set up WebP as your image format with the following code on the Android SDK:
cloudinary.url().transformation(new Transformation().width(500).height(333).crop("fill")).fetchFormat("webp")).generate("dog.jpg")
The corresponding URL ishttps://res.cloudinary.com/cld-name/image/upload/c_fill,f_webp,h_333,w_500/dog.jpg
, which you can manually construct.
Cloudinary’s image-quality setting, which defines the depth of lossy compression, also affects the ultimate look of the media. Finding the sweet spot for your images requires a test cycle, such as by setting the quality level to 80 and then to 90 to verify the image weight reduction and to determine which setting better meets your standard.
To avoid the chore of configuring each image’s optimal quality setting, use Cloudinary’s q_auto
option, which impeccably performs that job for you. For details, read this excellent article.
Automating the selection of image format and quality level with Cloudinary yields wonders. To further fine-tune the setup and save bandwidth, set the visual-quality level according to the delivery method. For example, leave the default “good” level (q_auto: good
) as is for websites and set the eco
level (q_auto: eco
) for native mobile apps.
For example, this image URL contains automated settings for websites:
To configure the same automation for an Android app, code as follows:
cloudinary.url().transformation(new Transformation().width(500).height(333).crop("fill")).fetchFormat("webp").quality("auto:eco")).generate("dog.jpg")
The equivalent URL is— https://res.cloudinary.com/cldname/image/upload/c_fill,f_webp,h_333,q_auto:eco,w_500/dog.jpg
Manually managing images is challenging, necessitating significant development effort to ensure a responsive design for images with different sizes to accommodate the various browsers. It makes a lot of sense to automate the process, especially since images now account for 60-65 percent of the average website content. Doing that on Cloudinary is simple and intuitive. Give it a try!
- Image Formats: Getting it Right
- The Great JPEG 2000 Debate: Analyzing the Pros and Cons to Widespread Adoption
- Progressive JPEGs and green Martians
- Animated WebP – how to convert animated GIF to WebP and save up to 90% bandwidth
- JPEG Image Optimization Without Compromising Quality With JPEGmini and Cloudinary
- Check for WebP Browser Support to Dynamically Deliver Images
- Optimizing Animated GIFs With Lossy Compression