Cloudinary Blog
Learn more...

Animated WebP - how to convert animated GIF to WebP and save up to 90% bandwidth

Jun 16, 2014 by Nadav Soferman

Animated WebP Fashion isn't something you'd expect to repeat itself in the technology world - technology advances quickly and hardly ever circles back. But where animated GIFs are involved, it seems like the 90s are here again. Animated GIFs are everywhere, and not only on strange, cheezy web sites - they've become mainstream. You now see short videos shared and played as animated GIFs in reputed sites such as Gawker and TechCrunch.

The GIF format is far from optimal for this purpose. While it's a simple image format supported by all web browsers and smartphones, converting videos to animated GIFs creates very large files. Downloading them takes time. Playing them uses a lot of memory and CPU resources. Pages on photo sharing sites, blogs, or even news sites, can become very slow when the page includes even a few animated GIFs that often weigh more than all the images in the page combined.

The GIF format was not meant to be used for full color video. It uses only 256 colors (with no semi-transparent alpha channel) and is better suited for drawings rather than photos or video frames. For a single photo frame, JPEG is a much better format both in quality and file size, but JPEG does not support animation.

A strong contender to replace the animated GIF is Google’s WebP format - with its new WebP animation support. In this blog post we show how you can use Cloudinary’s cloud-based image management service to convert animated GIFs on-the-fly to Animated WebP. In addition, we detail how we automatically deliver Animated WebP files only on supported browsers with graceful degradation to Animated GIFs otherwise. The end result is a full-color, high-quality animated images that weigh much less and are almost visually identical to the original animated GIF files.

Conversion from animated GIF to animated WebP

The following animated GIF of a bored person was uploaded to Cloudinary from Wikimedia. Although it is a relatively small image of 279x193, this short video weighs as much as 1.5MB. If you have 30 such images on your site, that's a total of 45MB, which takes time to load. And if you have 1000 daily visitors to your site, your monthly bandwidth for these images alone will be 1.3TB!

Ruby:
cl_image_tag("bored_animation.gif")
PHP:
cl_image_tag("bored_animation.gif")
Python:
CloudinaryImage("bored_animation.gif").image()
Node.js:
cloudinary.image("bored_animation.gif")
Java:
cloudinary.url().imageTag("bored_animation.gif")
jQuery:
$.cloudinary.image("bored_animation.gif")
.Net:
cloudinary.Api.UrlImgUp.BuildImageTag("bored_animation.gif")
Original animated GIF279x193  GIF  (1.53 MB)

Cloudinary is an image management service that stores your images on the cloud, and can convert and manipulate them on-the-fly. The code samples below, in all popular frameworks, construct a dynamic URL that converts the animated GIF above to an animated WebP. This URL enables the aweb flag (or fl_awebp if you’re providing the URL directly), and refers to the same image, bored_animation, changing its file extension to .webp (this tells Cloudinary you want to convert the image to this file format).

Ruby:
cl_image_tag("bored_animation.webp", :flags=>:awebp)
PHP:
cl_image_tag("bored_animation.webp", array("flags"=>"awebp"))
Python:
CloudinaryImage("bored_animation.webp").image(flags="awebp")
Node.js:
cloudinary.image("bored_animation.webp", {flags: "awebp"})
Java:
cloudinary.url().transformation(new Transformation().flags("awebp")).imageTag("bored_animation.webp")
jQuery:
$.cloudinary.image("bored_animation.webp", {flags: "awebp"})
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Flags("awebp")).BuildImageTag("bored_animation.webp")
Animated WebP279x193  WebP  (419 KB)

Note: Animated WebP is currently supported only by the Chrome browser. If you see a broken image here, try opening this blog post in Chrome (v32 or higher).

The WebP animation above is generated on-the-fly by Cloudinary when the dynamic URL is accessed. It looks the same as the original image, but weighs only 419KB. This means we saved 72% of file size, bandwidth and load time, compared to the original 1.5MB animated GIF.

By default, Cloudinary generates animated WebP in lossy mode. The default quality level, used for the animated WebP above, is 80%, but you can select a different quality level.

Let’s try to generate the same animated WebP with a lower quality level. We’ll set the quality parameter to 40 (q_40 when providing the URL directly). You can see the dynamic manipulation URL and the resulting WebP animation below.

Ruby:
cl_image_tag("bored_animation.webp", :quality=>40, :flags=>:awebp)
PHP:
cl_image_tag("bored_animation.webp", array("quality"=>40, "flags"=>"awebp"))
Python:
CloudinaryImage("bored_animation.webp").image(quality=40, flags="awebp")
Node.js:
cloudinary.image("bored_animation.webp", {quality: 40, flags: "awebp"})
Java:
cloudinary.url().transformation(new Transformation().quality(40).flags("awebp")).imageTag("bored_animation.webp")
jQuery:
$.cloudinary.image("bored_animation.webp", {quality: 40, flags: "awebp"})
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Quality(40).Flags("awebp")).BuildImageTag("bored_animation.webp")
Animated WebP of 40% quality279x193  WebP  (195 KB)

With quality set to 40%, the visual difference is almost unnoticeable, but the generated file weighs only 195KB. This saves 53% of size and bandwidth compared to the 80% quality animated WebP, and represents a size reduction of 87% compared to the original GIF.

Resizing and cropping animated GIFs and converting to animated WebP

You can use Cloudinary's image manipulation URLs to resize and crop animated GIFs. The following manipulation URL and resulting image generates a 150x100 north-filled rectangle of the same animated GIF. The resulting image, in which Cloudinary has already applied certain animated GIF optimizations, weighs 429KB.

Ruby:
cl_image_tag("bored_animation.gif", :width=>150, :height=>100, :crop=>:fill, :gravity=>:north)
PHP:
cl_image_tag("bored_animation.gif", array("width"=>150, "height"=>100, "crop"=>"fill", "gravity"=>"north"))
Python:
CloudinaryImage("bored_animation.gif").image(width=150, height=100, crop="fill", gravity="north")
Node.js:
cloudinary.image("bored_animation.gif", {width: 150, height: 100, crop: "fill", gravity: "north"})
Java:
cloudinary.url().transformation(new Transformation().width(150).height(100).crop("fill").gravity("north")).imageTag("bored_animation.gif")
jQuery:
$.cloudinary.image("bored_animation.gif", {width: 150, height: 100, crop: "fill", gravity: "north"})
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(150).Height(100).Crop("fill").Gravity("north")).BuildImageTag("bored_animation.gif")
Scaled down animated GIF150x100  GIF  (429 KB)

Now we can convert this image to animated WebP. The following URL generates the same 150x100 thumbnail, while converting to animated WebP with 80% quality. The result weighs only 154KB, which means we saved 64% of file size, without affecting quality.

Ruby:
cl_image_tag("bored_animation.webp", :quality=>80, :width=>150, :height=>100, :crop=>:fill, :gravity=>:north, :flags=>:awebp)
PHP:
cl_image_tag("bored_animation.webp", array("quality"=>80, "width"=>150, "height"=>100, "crop"=>"fill", "gravity"=>"north", "flags"=>"awebp"))
Python:
CloudinaryImage("bored_animation.webp").image(quality=80, width=150, height=100, crop="fill", gravity="north", flags="awebp")
Node.js:
cloudinary.image("bored_animation.webp", {quality: 80, width: 150, height: 100, crop: "fill", gravity: "north", flags: "awebp"})
Java:
cloudinary.url().transformation(new Transformation().quality(80).width(150).height(100).crop("fill").gravity("north").flags("awebp")).imageTag("bored_animation.webp")
jQuery:
$.cloudinary.image("bored_animation.webp", {quality: 80, width: 150, height: 100, crop: "fill", gravity: "north", flags: "awebp"})
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Quality(80).Width(150).Height(100).Crop("fill").Gravity("north").Flags("awebp")).BuildImageTag("bored_animation.webp")
Scaled down animated WebP150x100  WebP  (154 KB)

The animated WebP format is also very useful for animations of drawings, not only for video. The examples below show an animated drawing uploaded to Cloudinary from Wikimedia. The original animated GIF on the left weighs 790KB, while the WebP version on the right, at 80% quality, weighs only 375KB (a reduction of 52.5%).

Ruby:
cl_image_tag("cell_animation.webp", :flags=>:awebp)
PHP:
cl_image_tag("cell_animation.webp", array("flags"=>"awebp"))
Python:
CloudinaryImage("cell_animation.webp").image(flags="awebp")
Node.js:
cloudinary.image("cell_animation.webp", {flags: "awebp"})
Java:
cloudinary.url().transformation(new Transformation().flags("awebp")).imageTag("cell_animation.webp")
jQuery:
$.cloudinary.image("cell_animation.webp", {flags: "awebp"})
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Flags("awebp")).BuildImageTag("cell_animation.webp")
Animated WebP drawing320x240  WebP  (375 KB) Animated GIF drawing320x240  GIF  (790 KB)

Automatically deliver animated WebP only on supported browsers

Hopefully, we’ve managed to show that animated WebP can be far superior to animated GIF. The problem is that only the Google Chrome browser, version 32 or higher, supports this format. Chrome is the leading browser in popularity and pushes upgrades strongly; for example, more than 60% of Cloudinary's website visitors use Chrome v32 or higher. It’s likely that on your website too, Chrome users are a sufficiently large audience to justify serving WebP images.

The challenge is to deliver WebP images to users of supported browsers, and deliver other formats (GIF, PNG, JPG, etc.) to users of other browsers. In our case, we need to deliver animated WebP to users of Chrome v32 or higher, and animated GIF to all other users.

Cloudinary can help by automatically delivering the appropriate image format to each user based on their browser (see our previous post which discusses this feature in more depth). To do this for an animated image like the one below, just set the fetch_format parameter to auto (or f_auto for URLs). Cloudinary automatically detects the user browser at the CDN level, and delivers cached and optimized converted animated WebP, or the original animated GIF, according to the user's browser.

Ruby:
cl_image_tag("bored_animation.gif", :width=>150, :height=>100, :crop=>:fill, :gravity=>:north)
PHP:
cl_image_tag("bored_animation.gif", array("width"=>150, "height"=>100, "crop"=>"fill", "gravity"=>"north"))
Python:
CloudinaryImage("bored_animation.gif").image(width=150, height=100, crop="fill", gravity="north")
Node.js:
cloudinary.image("bored_animation.gif", {width: 150, height: 100, crop: "fill", gravity: "north"})
Java:
cloudinary.url().transformation(new Transformation().width(150).height(100).crop("fill").gravity("north")).imageTag("bored_animation.gif")
jQuery:
$.cloudinary.image("bored_animation.gif", {width: 150, height: 100, crop: "fill", gravity: "north"})
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(150).Height(100).Crop("fill").Gravity("north")).BuildImageTag("bored_animation.gif")
WebP or GIF automatically generated and delivered

Further manipulations on your animated WebP images

We showed above how to resize and crop an animated image in Cloudinary. But there are many more manipulations you can apply to an animated GIF or WebP image. The following example uses Cloudinary's image manipulation URLs to round the corners of the animated GIF, add a gray border and a semi-transparent watermark overlay of another uploaded image, and finally convert to animated WebP. Again - the WebP format generates a visually-identical result that weighs just 35% of the GIF version (saving 65% of size and bandwidth).

Ruby:
cl_image_tag("bored_animation.webp", :transformation=>[
  {:radius=>20, :border=>"3px_solid_rgb:aaa", :width=>150, :height=>100, :crop=>:fill, :gravity=>:north},
  {:opacity=>50, :overlay=>"cloudinary_icon", :width=>0.8, :crop=>:scale, :flags=>:relative},
  {:quality=>80, :flags=>:awebp}
  ])
PHP:
cl_image_tag("bored_animation.webp", array("transformation"=>array(
  array("radius"=>20, "border"=>"3px_solid_rgb:aaa", "width"=>150, "height"=>100, "crop"=>"fill", "gravity"=>"north"),
  array("opacity"=>50, "overlay"=>"cloudinary_icon", "width"=>0.8, "crop"=>"scale", "flags"=>"relative"),
  array("quality"=>80, "flags"=>"awebp")
  )))
Python:
CloudinaryImage("bored_animation.webp").image(transformation=[
  {"radius": 20, "border": "3px_solid_rgb:aaa", "width": 150, "height": 100, "crop": "fill", "gravity": "north"},
  {"opacity": 50, "overlay": "cloudinary_icon", "width": 0.8, "crop": "scale", "flags": "relative"},
  {"quality": 80, "flags": "awebp"}
  ])
Node.js:
cloudinary.image("bored_animation.webp", {transformation: [
  {radius: 20, border: "3px_solid_rgb:aaa", width: 150, height: 100, crop: "fill", gravity: "north"},
  {opacity: 50, overlay: "cloudinary_icon", width: 0.8, crop: "scale", flags: "relative"},
  {quality: 80, flags: "awebp"}
  ]})
Java:
cloudinary.url().transformation(new Transformation()
  .radius(20).border("3px_solid_rgb:aaa").width(150).height(100).crop("fill").gravity("north").chain()
  .opacity(50).overlay("cloudinary_icon").width(0.8).crop("scale").flags("relative").chain()
  .quality(80).flags("awebp")).imageTag("bored_animation.webp")
jQuery:
$.cloudinary.image("bored_animation.webp", {transformation: [
  {radius: 20, border: "3px_solid_rgb:aaa", width: 150, height: 100, crop: "fill", gravity: "north"},
  {opacity: 50, overlay: "cloudinary_icon", width: 0.8, crop: "scale", flags: "relative"},
  {quality: 80, flags: "awebp"}
  ]})
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation()
  .Radius(20).Border("3px_solid_rgb:aaa").Width(150).Height(100).Crop("fill").Gravity("north").Chain()
  .Opacity(50).Overlay("cloudinary_icon").Width(0.8).Crop("scale").Flags("relative").Chain()
  .Quality(80).Flags("awebp")).BuildImageTag("bored_animation.webp")
Manipulated animated WebP156x106  WebP  (233 KB) Manipulated animated GIF156x106  GIF  (664 KB)

Summary

The results of switching from animated GIF to animated WebP are impressive - in the examples we showed above, image size and bandwidth were reduced by 65-90%, with the same visual result. Albeit, WebP is only used by a limited number of browsers, but the latest versions of Chrome are becoming more and more popular, and so the importance of WebP is on the rise.

With Cloudinary's automatic conversion and selective delivery of animation format based on the user's browser, there's definitely no reason not to use animated WebP for displaying your short videos. Furthermore, if you convert your original high quality videos to WebP (and not just convert from GIF to WebP like we did in the examples above), you’ll get a better visual result, due to the WebP format’s full-color support, compared to only 256 colors in an animated GIF.

By the way, in our humble opinion, those major sites that are now full of animated GIFs, all playing simultaneously, could achieve a more professional result by showing the first still frame of the animation, with a small play button. With Cloudinary you can easily convert an animated GIF or WebP animation to any single-frame image format. For example:

Ruby:
cl_image_tag("bored_animation.jpg", :transformation=>[
  {:width=>150, :height=>100, :crop=>:fill, :gravity=>:north},
  {:opacity=>60, :overlay=>"play_button", :width=>0.4, :crop=>:scale, :flags=>:relative}
  ])
PHP:
cl_image_tag("bored_animation.jpg", array("transformation"=>array(
  array("width"=>150, "height"=>100, "crop"=>"fill", "gravity"=>"north"),
  array("opacity"=>60, "overlay"=>"play_button", "width"=>0.4, "crop"=>"scale", "flags"=>"relative")
  )))
Python:
CloudinaryImage("bored_animation.jpg").image(transformation=[
  {"width": 150, "height": 100, "crop": "fill", "gravity": "north"},
  {"opacity": 60, "overlay": "play_button", "width": 0.4, "crop": "scale", "flags": "relative"}
  ])
Node.js:
cloudinary.image("bored_animation.jpg", {transformation: [
  {width: 150, height: 100, crop: "fill", gravity: "north"},
  {opacity: 60, overlay: "play_button", width: 0.4, crop: "scale", flags: "relative"}
  ]})
Java:
cloudinary.url().transformation(new Transformation()
  .width(150).height(100).crop("fill").gravity("north").chain()
  .opacity(60).overlay("play_button").width(0.4).crop("scale").flags("relative")).imageTag("bored_animation.jpg")
jQuery:
$.cloudinary.image("bored_animation.jpg", {transformation: [
  {width: 150, height: 100, crop: "fill", gravity: "north"},
  {opacity: 60, overlay: "play_button", width: 0.4, crop: "scale", flags: "relative"}
  ]})
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation()
  .Width(150).Height(100).Crop("fill").Gravity("north").Chain()
  .Opacity(60).Overlay("play_button").Width(0.4).Crop("scale").Flags("relative")).BuildImageTag("bored_animation.jpg")
First animation frame with play button150x100  JPEG  (5.01 KB)

Dynamic manipulation of animated GIFs, on-the-fly conversion to Web and all other powerful image manipulation and optimization features described in this post, are available for all Cloudinary's users, including those using the free plan. Set up a free Cloudinary account to try it out. And we’ll more than welcome your feedback on this feature via the comments below or your favorite social network.

blog comments powered by Disqus