Cloudinary Blog

Adopting the WebP Image Format for Android on Websites Or Native Apps

Using WebP for Android on Your Website or Native Apps

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.

Benefits of WebP for Android

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. Talk about dynamite!

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.


How to Optimize for Page Load Speed


WebP on Mobile Android Apps

As a matter of course, Android being an OS developed by Google, WebP works well on Android. Specifically:

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: 300x190. Quality: 80)

JPEG JPEG(15KB) WebP WebP(10KB)

PNG -> WebP (Resolution: 300 x 192. Image with transparency)

PNG PNG(55KB) WebP WebP(39KB)

A GIF -> A WebP (Resolution: 300x168. Quality: 80. Animation)

GIF GIF(2.23MB) Animated WebP Animated WebP(887KB)

Adoption of WebP With <picture> Tag

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.

Automation of Image-Format Conversion on Cloudinary

In the case of multiple images, rather than specifying a WebP variant for each of them, take advantage of Cloudinary’s automation capability, 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.

Ruby:
Copy to clipboard
cl_image_tag("dog2.jpg", :width=>500, :height=>333, :crop=>"fill", :fetch_format=>:auto)
PHP:
Copy to clipboard
cl_image_tag("dog2.jpg", array("width"=>500, "height"=>333, "crop"=>"fill", "fetch_format"=>"auto"))
Python:
Copy to clipboard
CloudinaryImage("dog2.jpg").image(width=500, height=333, crop="fill", fetch_format="auto")
Node.js:
Copy to clipboard
cloudinary.image("dog2.jpg", {width: 500, height: 333, crop: "fill", fetch_format: "auto"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().width(500).height(333).crop("fill").fetchFormat("auto")).imageTag("dog2.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('dog2.jpg', {width: 500, height: 333, crop: "fill", fetchFormat: "auto"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("dog2.jpg", {width: 500, height: 333, crop: "fill", fetch_format: "auto"})
React:
Copy to clipboard
<Image publicId="dog2.jpg" >
  <Transformation width="500" height="333" crop="fill" fetchFormat="auto" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="dog2.jpg" >
  <cld-transformation width="500" height="333" crop="fill" fetchFormat="auto" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="dog2.jpg" >
  <cl-transformation width="500" height="333" crop="fill" fetch-format="auto">
  </cl-transformation>
</cl-image>
.Net:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(500).Height(333).Crop("fill").FetchFormat("auto")).BuildImageTag("dog2.jpg")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().width(500).height(333).crop("fill").fetchFormat("auto")).generate("dog2.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setWidth(500).setHeight(333).setCrop("fill").setFetchFormat("auto")).generate("dog2.jpg")!, cloudinary: cloudinary)
Dog

Support of WebP on Native Mobile Apps

For native mobile apps on Android, set up WebP as your image format with the following code on the Android SDK:

Copy to clipboard
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.

A Quality Boost

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.

Automation of Quality Adjustments for Full Automation

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:

Ruby:
Copy to clipboard
cl_image_tag("dog2.jpg", :width=>500, :height=>333, :quality=>"auto", :crop=>"fill", :fetch_format=>:auto)
PHP:
Copy to clipboard
cl_image_tag("dog2.jpg", array("width"=>500, "height"=>333, "quality"=>"auto", "crop"=>"fill", "fetch_format"=>"auto"))
Python:
Copy to clipboard
CloudinaryImage("dog2.jpg").image(width=500, height=333, quality="auto", crop="fill", fetch_format="auto")
Node.js:
Copy to clipboard
cloudinary.image("dog2.jpg", {width: 500, height: 333, quality: "auto", crop: "fill", fetch_format: "auto"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().width(500).height(333).quality("auto").crop("fill").fetchFormat("auto")).imageTag("dog2.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('dog2.jpg', {width: 500, height: 333, quality: "auto", crop: "fill", fetchFormat: "auto"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("dog2.jpg", {width: 500, height: 333, quality: "auto", crop: "fill", fetch_format: "auto"})
React:
Copy to clipboard
<Image publicId="dog2.jpg" >
  <Transformation width="500" height="333" quality="auto" crop="fill" fetchFormat="auto" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="dog2.jpg" >
  <cld-transformation width="500" height="333" quality="auto" crop="fill" fetchFormat="auto" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="dog2.jpg" >
  <cl-transformation width="500" height="333" quality="auto" crop="fill" fetch-format="auto">
  </cl-transformation>
</cl-image>
.Net:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(500).Height(333).Quality("auto").Crop("fill").FetchFormat("auto")).BuildImageTag("dog2.jpg")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().width(500).height(333).quality("auto").crop("fill").fetchFormat("auto")).generate("dog2.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setWidth(500).setHeight(333).setQuality("auto").setCrop("fill").setFetchFormat("auto")).generate("dog2.jpg")!, cloudinary: cloudinary)
Dog

To configure the same automation for an Android app, code as follows:

Copy to clipboard
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

Summary

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!

Want to Learn More About Image Formats?

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