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

A New, Simple Tool for Creating Text Overlays for Images

Many Cloudinary users desire a UI for tasks like creating text overlays for images, which they then embed on webpages or download for marketing campaigns. Generating such overlays with the Cloudinary Media Library UI involves a bit of a learning curve, especially if they require multiple fonts or text lines, which even experienced users might find challenging to implement.

Read more
Transitioning JPEG-Based to JPEG XL-Based Images for Web Platforms

When the JPEG codec was being developed in the late 1980s, no standardized, lossy image-compression formats existed. JPEG became ready at exactly the right time in 1992, when the World Wide Web and digital cameras were about to become a thing. The introduction of HTML’s <img> tag in 1995 ensured the recognition of JPEG as the web format—at least for photographs. During the 1990s, digital cameras replaced analog ones and, given the limited memory capacities of that era, JPEG became the standard format for photography, especially for consumer-grade cameras.

Read more

Amplify Your Jamstack With Video

By Alex Patterson
Amplify Your Jamstack With Cloudinary Video

As defined by Amazon Web Services (AWS), Amplify is a set of products and tools with which mobile and front-end web developers can build and deploy AWS-powered, secure, and scalable full-stack apps. Also, you can efficiently configure their back ends, connect them to your app with just a few lines of code, and deploy static web apps in only three steps. Historically, because of their performance issues, managing images and videos is a daunting challenge for developers. Even though you can easily load media to an S3 bucket with AWS Amplify, transforming, compressing, and responsively delivering them is labor intensive and time consuming.

Read more
Cloudinary Helps Move James Hardie’s Experience Online

While COVID has affected most businesses, it has been particularly hard on those that sell products for the physical ‘brick and mortar’ world. One company that literally fits that bill is our Australian customer James Hardie, the largest global manufacturer of fibre cement products used in both domestic and commercial construction. These are materials that its buyers ideally want to see up close, in detail. When customers have questions, they expect personal service.

Read more