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 surveyoptimization 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:
cl_image_tag("dog2.jpg", :width=>500, :height=>333, :crop=>"fill", :fetch_format=>:auto)
PHP:
cl_image_tag("dog2.jpg", array("width"=>500, "height"=>333, "crop"=>"fill", "fetch_format"=>"auto"))
Python:
CloudinaryImage("dog2.jpg").image(width=500, height=333, crop="fill", fetch_format="auto")
Node.js:
cloudinary.image("dog2.jpg", {width: 500, height: 333, crop: "fill", fetch_format: "auto"})
Java:
cloudinary.url().transformation(new Transformation().width(500).height(333).crop("fill").fetchFormat("auto")).imageTag("dog2.jpg");
JS:
cloudinary.imageTag('dog2.jpg', {width: 500, height: 333, crop: "fill", fetchFormat: "auto"}).toHtml();
jQuery:
$.cloudinary.image("dog2.jpg", {width: 500, height: 333, crop: "fill", fetch_format: "auto"})
React:
<Image publicId="dog2.jpg" >
  <Transformation width="500" height="333" crop="fill" fetchFormat="auto" />
</Image>
Angular:
<cl-image public-id="dog2.jpg" >
  <cl-transformation width="500" height="333" crop="fill" fetch-format="auto">
  </cl-transformation>
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(500).Height(333).Crop("fill").FetchFormat("auto")).BuildImageTag("dog2.jpg")
Android:
MediaManager.get().url().transformation(new Transformation().width(500).height(333).crop("fill").fetchFormat("auto")).generate("dog2.jpg");
iOS:
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:

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:
cl_image_tag("dog2.jpg", :width=>500, :height=>333, :quality=>"auto", :crop=>"fill", :fetch_format=>:auto)
PHP:
cl_image_tag("dog2.jpg", array("width"=>500, "height"=>333, "quality"=>"auto", "crop"=>"fill", "fetch_format"=>"auto"))
Python:
CloudinaryImage("dog2.jpg").image(width=500, height=333, quality="auto", crop="fill", fetch_format="auto")
Node.js:
cloudinary.image("dog2.jpg", {width: 500, height: 333, quality: "auto", crop: "fill", fetch_format: "auto"})
Java:
cloudinary.url().transformation(new Transformation().width(500).height(333).quality("auto").crop("fill").fetchFormat("auto")).imageTag("dog2.jpg");
JS:
cloudinary.imageTag('dog2.jpg', {width: 500, height: 333, quality: "auto", crop: "fill", fetchFormat: "auto"}).toHtml();
jQuery:
$.cloudinary.image("dog2.jpg", {width: 500, height: 333, quality: "auto", crop: "fill", fetch_format: "auto"})
React:
<Image publicId="dog2.jpg" >
  <Transformation width="500" height="333" quality="auto" crop="fill" fetchFormat="auto" />
</Image>
Angular:
<cl-image public-id="dog2.jpg" >
  <cl-transformation width="500" height="333" quality="auto" crop="fill" fetch-format="auto">
  </cl-transformation>
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(500).Height(333).Quality("auto").Crop("fill").FetchFormat("auto")).BuildImageTag("dog2.jpg")
Android:
MediaManager.get().url().transformation(new Transformation().width(500).height(333).quality("auto").crop("fill").fetchFormat("auto")).generate("dog2.jpg");
iOS:
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:

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!

Further Reading on Image Optimization

Recent Blog Posts

Converting Videos to GIFs in Kotlin-Based Android Apps

Among the image formats available today, Graphics Interchange Format (GIF) works for both static and animated images. A popular use of GIF images—commonly called GIFs—is to repeatedly loop motions, which you cannot in real life. Many people have turned themselves into online celebrities through effective application of GIFs.

Read more
Simple Steps to Make Your Site Accessible With Cloudinary

Web accessibility (a11y) is about gaining an insight into—

It’s a blurry line between accessibility and usability (or user experience) and one might argue that they are actually the same thing. Nonetheless, if your audience can't use your app with ease and confidence, then it’s not accessible enough and the user experience is not good enough.

Read more
Learn three ways in which to tell appealing stories on video.

Video storytelling is one of the most effective means of conveying messages to your audience. It’s also a popular marketing tool because most people are automatically drawn to video. In eCommerce, if accompanied by attractive images and a compelling narrative, video storytelling uniquely connect with potential customers without fail.

Read more