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.

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, :quality=>"auto", :crop=>"fill", :fetch_format=>:auto)
PHP v1:
Copy to clipboard
cl_image_tag("dog2.jpg", array("width"=>500, "height"=>333, "quality"=>"auto", "crop"=>"fill", "fetch_format"=>"auto"))
PHP v2:
Copy to clipboard
(new ImageTag('dog2.jpg'))
  ->resize(Resize::fill()->width(500)->height(333))
  ->delivery(Delivery::format(Format::auto()))
  ->delivery(Delivery::quality(Quality::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

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 v1:
Copy to clipboard
cl_image_tag("dog2.jpg", array("width"=>500, "height"=>333, "quality"=>"auto", "crop"=>"fill", "fetch_format"=>"auto"))
PHP v2:
Copy to clipboard
(new ImageTag('dog2.jpg'))
  ->resize(Resize::fill()->width(500)->height(333))
  ->delivery(Delivery::format(Format::auto()))
  ->delivery(Delivery::quality(Quality::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

Our $2B Valuation

By
Blackstone Growth Invests in Cloudinary

When we started our journey in 2012, we were looking to improve our lives as developers by making it easier for us to handle the arduous tasks of handling images and videos in our code. That initial line of developer code has evolved into a full suite of media experience solutions driven by a mission that gradually revealed itself over the course of the past 10 years: help companies unleash the full potential of their media to create the most engaging visual experiences.

Read more
Direct-to-Consumer E-Commerce Requires Compelling Visual Experiences

When brands like you adopt a direct–to-consumer (DTC) e-commerce approach with no involvement of retailers or marketplaces, you gain direct and timely insight into evolving shopping behaviors. Accordingly, you can accommodate shoppers’ preferences by continually adjusting your product offering and interspersing the shopping journey with moments of excitement and intrigue. Opportunities abound for you to cultivate engaging customer relationships.

Read more
Automatically Translating Videos for an International Audience

No matter your business focus—public service, B2B integration, recruitment—multimedia, in particular video, is remarkably effective in communicating with the audience. Before, making video accessible to diverse viewers involved tasks galore, such as eliciting the service of production studios to manually dub, transcribe, and add subtitles. Those operations were costly and slow, especially for globally destined content.

Read more
Cloudinary Helps Minted Manage Its Image-Generation Pipeline at Scale

Shoppers return time and again to Minted’s global online community of independent artists and designers because they know they can count on unique, statement-making products of the highest quality there. Concurrently, the visual imagery on Minted.com must do justice to the designs into which the creators have poured their hearts and souls. For Minted’s VP of Engineering David Lien, “Because we are a premium brand, we need to ensure that every single one of our product images matches the selected configuration exactly. For example, if you pick an 18x24 art print on blue canvas, we will show that exact combination on the hero images in the PDF.”

Read more
Highlights on ImageCon 2021 and a Preview of ImageCon 2022

New year, same trend! Visual media will continue to play a monumental role in driving online conversions. To keep up with visual-experience trends and best practices, Cloudinary holds an annual conference called ImageCon, a one-of-a-kind event that helps attendees create the most engaging visual experiences possible.

Read more