Cloudinary Blog

Adaptive browser based image format delivery

Automatically deliver the best image format to the browser

One of the main optimization challenges for website and mobile developers is how to display sufficiently high quality images to their visitors while minimizing the image file size. A smaller image file size can lead to faster load times, reduced bandwidth costs and an improved user experience. The problem is that reducing the file size too much may lead to a lower image quality and could harm visitor satisfaction. Delivering an optimized image with just the right balance between size and quality can be quite tricky.

Selecting the optimal image format - f_auto

Some formats such as WebP and JPEG-XR are more efficient than the standard JPEG format for delivering web images, but they are not supported by all browsers: JPEG-XR is supported only in Internet Explorer/Edge browsers, while WebP is currently supported only in Chrome/Opera browsers and Android devices. The result is that the best image to deliver to your visitor depends on which browser they are using.

Cloudinary has the ability to automatically detect which browser is requesting the image and then select the most efficient image format to deliver. Just add the fetch_format parameter and set its value to auto (f_auto in URLs).

The example below displays two sample images. The image on the left uses a URL without Cloudinary's f_auto flag and is therefore delivered in JPEG format across all browsers (while being scaled on-the-fly to 300px width with its aspect ratio retained). The image on the right includes f_auto in its delivery URL and so is delivered as WebP (9.8 KB - a saving of 51%) if you are using a Chrome or Opera browser, as a JPEG-XR (12.6 KB - a saving of 37%) to Internet Explorer, Edge, and Android browsers, and as a JPEG (20 KB) in all other cases.

JPEG to all browsers

WebP to Chrome, JPEG-XR to IE and JPEG to all others

Ruby:
cl_image_tag("pond_reflect.jpg", :width=>300, :fetch_format=>:auto, :crop=>"scale")
PHP:
cl_image_tag("pond_reflect.jpg", array("width"=>300, "fetch_format"=>"auto", "crop"=>"scale"))
Python:
CloudinaryImage("pond_reflect.jpg").image(width=300, fetch_format="auto", crop="scale")
Node.js:
cloudinary.image("pond_reflect.jpg", {width: 300, fetch_format: "auto", crop: "scale"})
Java:
cloudinary.url().transformation(new Transformation().width(300).fetchFormat("auto").crop("scale")).imageTag("pond_reflect.jpg")
JS:
cl.imageTag('pond_reflect.jpg', {width: 300, fetch_format: "auto", crop: "scale"}).toHtml();
jQuery:
$.cloudinary.image("pond_reflect.jpg", {width: 300, fetch_format: "auto", crop: "scale"})
React:
<Image publicId="pond_reflect.jpg" width="300" fetch_format="auto" crop="scale">
        <Transformation width=300 fetch_format=:auto crop="scale" />
</Image>
Angular:
<cl-image public-id="pond_reflect.jpg" width="300" fetch_format="auto" crop="scale">
        <cl-transformation width=300 fetch_format=:auto crop="scale" />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(300).FetchFormat("auto").Crop("scale")).BuildImageTag("pond_reflect.jpg")

The example above demonstrates automatic format selection for images that were uploaded to Cloudinary first, either using our upload API from your server code or directly from your visitor's browsers or mobile apps.

In addition to direct uploads, Cloudinary also supports fetching images via their public URLs, manipulating these on-the-fly and delivering the resulting images optimized via a CDN. This means, for example, that you can easily integrate Cloudinary with your website without modifying your infrastructure and code. Simply prefix your image URLs with Cloudinary's fetch URL.

With Cloudinary's automatic format feature, you can also dynamically convert and deliver remote images and improve your site's performance.

For example, the following URL delivers a picture of Dwayne Johnson from a remote Wikimedia Commons HTTP URL. The remote image is fetched by Cloudinary, stored persistently in the cloud, dynamically converted to WebP or JPEG-XR as required by the user's browser and delivered optimized and cached through a high-end CDN.

Ruby:
cl_image_tag("https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg", :width=>300, :fetch_format=>:auto, :crop=>"scale", :type=>"fetch")
PHP:
cl_image_tag("https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg", array("width"=>300, "fetch_format"=>"auto", "crop"=>"scale", "type"=>"fetch"))
Python:
CloudinaryImage("https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg").image(width=300, fetch_format="auto", crop="scale", type="fetch")
Node.js:
cloudinary.image("https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg", {width: 300, fetch_format: "auto", crop: "scale", type: "fetch"})
Java:
cloudinary.url().transformation(new Transformation().width(300).fetchFormat("auto").crop("scale")).type("fetch").imageTag("https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg")
JS:
cl.imageTag('https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg', {width: 300, fetch_format: "auto", crop: "scale", type: "fetch"}).toHtml();
jQuery:
$.cloudinary.image("https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg", {width: 300, fetch_format: "auto", crop: "scale", type: "fetch"})
React:
<Image publicId="https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg" width="300" fetch_format="auto" crop="scale" type="fetch">
        <Transformation width=300 fetch_format=:auto crop="scale" />
</Image>
Angular:
<cl-image public-id="https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg" width="300" fetch_format="auto" crop="scale" type="fetch">
        <cl-transformation width=300 fetch_format=:auto crop="scale" />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(300).FetchFormat("auto").Crop("scale")).Type("fetch").BuildImageTag("https://upload.wikimedia.org/wikipedia/commons/6/68/Dwayne_Johnson_at_the_2009_Tribeca_Film_Festival.jpg")
Remote fetched image with automatic format selection

Optimizing image quality

Cloudinary's dynamic format selection feature can be further enhanced by using it together with Cloudinary's automatic quality selection feature. Including the quality parameter and setting its value to auto (q_auto in URLs) tells Cloudinary to automatically determine the optimum quality setting for an image based on its format and contents, that results in the smallest file size while maintaining visual quality. When you add f_auto together with q_auto in the delivery URL, the Cloudinary algorithm also checks if a different image format gives a smaller file size while maintaining the visual quality.

For example, the canyons image scaled down to a width of 400 pixels and delivered with both automatic quality selection and automatic format selection (q_auto and f_auto), will be delivered as WebP (20.2KB - a saving of 45%) to Chrome browsers, JPEG-XR (24.3KB - a saving of 34%) to Internet-Explorer/Edge browsers, and JPEG (36.6KB) to all other browsers:

Ruby:
cl_image_tag("canyons.jpg", :width=>400, :quality=>"auto", :fetch_format=>:auto, :crop=>"scale")
PHP:
cl_image_tag("canyons.jpg", array("width"=>400, "quality"=>"auto", "fetch_format"=>"auto", "crop"=>"scale"))
Python:
CloudinaryImage("canyons.jpg").image(width=400, quality="auto", fetch_format="auto", crop="scale")
Node.js:
cloudinary.image("canyons.jpg", {width: 400, quality: "auto", fetch_format: "auto", crop: "scale"})
Java:
cloudinary.url().transformation(new Transformation().width(400).quality("auto").fetchFormat("auto").crop("scale")).imageTag("canyons.jpg")
JS:
cl.imageTag('canyons.jpg', {width: 400, quality: "auto", fetch_format: "auto", crop: "scale"}).toHtml();
jQuery:
$.cloudinary.image("canyons.jpg", {width: 400, quality: "auto", fetch_format: "auto", crop: "scale"})
React:
<Image publicId="canyons.jpg" width="400" quality="auto" fetch_format="auto" crop="scale">
        <Transformation width=400 quality="auto" fetch_format=:auto crop="scale" />
</Image>
Angular:
<cl-image public-id="canyons.jpg" width="400" quality="auto" fetch_format="auto" crop="scale">
        <cl-transformation width=400 quality="auto" fetch_format=:auto crop="scale" />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(400).Quality("auto").FetchFormat("auto").Crop("scale")).BuildImageTag("canyons.jpg")
Optimized image quality and format

Furthermore, when using f_auto and q_auto together, not only is the visitor's browser taken into account when deciding on the best image format, but also the image contents. For example, the Cloudinary algorithm might determine that the PNG format is a better fit for specific images that contain content such as drawings. For some images, even the PNG8 format can be automatically selected for providing great looking results with a very efficient file size.

For example, the following URL dynamically generates a 400 pixels wide version of a drawing only using automatic image quality selection (q_auto without f_auto).

Ruby:
cl_image_tag("cloud_castle.jpg", :width=>400, :quality=>"auto", :crop=>"scale")
PHP:
cl_image_tag("cloud_castle.jpg", array("width"=>400, "quality"=>"auto", "crop"=>"scale"))
Python:
CloudinaryImage("cloud_castle.jpg").image(width=400, quality="auto", crop="scale")
Node.js:
cloudinary.image("cloud_castle.jpg", {width: 400, quality: "auto", crop: "scale"})
Java:
cloudinary.url().transformation(new Transformation().width(400).quality("auto").crop("scale")).imageTag("cloud_castle.jpg")
JS:
cl.imageTag('cloud_castle.jpg', {width: 400, quality: "auto", crop: "scale"}).toHtml();
jQuery:
$.cloudinary.image("cloud_castle.jpg", {width: 400, quality: "auto", crop: "scale"})
React:
<Image publicId="cloud_castle.jpg" width="400" quality="auto" crop="scale">
        <Transformation width=400 quality="auto" crop="scale" />
</Image>
Angular:
<cl-image public-id="cloud_castle.jpg" width="400" quality="auto" crop="scale">
        <cl-transformation width=400 quality="auto" crop="scale" />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(400).Quality("auto").Crop("scale")).BuildImageTag("cloud_castle.jpg")
Drawing with automatic quality

The result is a JPEG image (20.4KB) where, if you look carefully, you can see that the lossy nature of the JPEG format resulted in some visual artifacts. In the next example with the same drawing, we will combine both q_auto and f_auto:

Ruby:
cl_image_tag("cloud_castle.jpg", :width=>400, :quality=>"auto", :fetch_format=>:auto, :crop=>"scale")
PHP:
cl_image_tag("cloud_castle.jpg", array("width"=>400, "quality"=>"auto", "fetch_format"=>"auto", "crop"=>"scale"))
Python:
CloudinaryImage("cloud_castle.jpg").image(width=400, quality="auto", fetch_format="auto", crop="scale")
Node.js:
cloudinary.image("cloud_castle.jpg", {width: 400, quality: "auto", fetch_format: "auto", crop: "scale"})
Java:
cloudinary.url().transformation(new Transformation().width(400).quality("auto").fetchFormat("auto").crop("scale")).imageTag("cloud_castle.jpg")
JS:
cl.imageTag('cloud_castle.jpg', {width: 400, quality: "auto", fetch_format: "auto", crop: "scale"}).toHtml();
jQuery:
$.cloudinary.image("cloud_castle.jpg", {width: 400, quality: "auto", fetch_format: "auto", crop: "scale"})
React:
<Image publicId="cloud_castle.jpg" width="400" quality="auto" fetch_format="auto" crop="scale">
        <Transformation width=400 quality="auto" fetch_format=:auto crop="scale" />
</Image>
Angular:
<cl-image public-id="cloud_castle.jpg" width="400" quality="auto" fetch_format="auto" crop="scale">
        <cl-transformation width=400 quality="auto" fetch_format=:auto crop="scale" />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(400).Quality("auto").FetchFormat("auto").Crop("scale")).BuildImageTag("cloud_castle.jpg")
Drawing with automatic quality and format

In this case, the algorithm decided to encode the image using the PNG8 format. The image looks better, has no artifacts, and weighs even less - just 16.5KB

Summary - automatic format delivery

Delivering an image in the best format can be easily automated with Cloudinary's format selection algorithm. The feature can also be combined with automatic quality selection for a powerful and dynamic solution that delivers all your images using minimal bandwidth and maximum visual quality. For more details, see the automatic format selection documentation.

All image manipulation and delivery features discussed here are available with no extra charge for all Cloudinary's plans, including the free plan.

Recent Blog Posts

Serverless Tutorial: File Storage with Webtask and Cloudinary

Media makes up the majority of today's website content. While it makes websites more interesting for visitors, media presents challenges because these images and videos are more difficult to store, deliver and manipulate on-the-fly to suit any given situation.

Read more

ImageCon17: Delivering Responsive Images

By Jason Grigsby

After five years many specifications, some inflamed Twitter battles and other conversations, responsive images have finally landed and there's a sound. Which is really exciting right? People have been climbing for this for quite some time and we've reached a point where they're available in modern browsers. So people were excited, they wanted to go use them it's something that designers and developers have had as a point of frustration for a long time.

Read more
Auto padding images with content-aware color padding

How you present the content of your website can be just as important as the content itself. The images you display need to conform to the graphic design of your site, and every image needs to fit within a predefined size. Although that may be simple enough to achieve when you are dealing with your own images, the task can be more challenging when displaying images uploaded by your users.

Read more

Bounce! Hacking Jazzfest with Social Videos

By Eric Normand
Bounce! Hacking Jazzfest with Social Videos

Last week, I was invited to an exclusive hackathon to build apps for musicians. The app team I was assigned to was tasked with building a video upload site for Bounce videos. Bounce is a style of music that originated in New Orleans. The app would be called BounceDotCom.com and there were plans to have Big Freedia, the Queen of Bounce, promote it. I knew the organizer could make things happen, so I jumped at the chance.

Read more
Getting a Better React-ion with Progressive Web Apps

This is part 2 of a 3 part series

React has become more popular, as well as more mature, over the last four years since its release by Facebook. It has become one of the go-to technologies for people looking to componentize the front-end of any web application. It also helps that an entire mobile stack is built around React in the form of ReactNative. The components are wonderful, however there can be a burdensome learning curve. But, in the end, there’s the payoff of highly reusable code and a better user experience.

Read more
Build an Image Library with React & Cloudinary

This article was originally posted on Scotch.io

React is a good tool when it comes to building flexible and reusable UI components. However, it's "one of those libraries" that cannot handle all the tasks involved in building a full fleshed UI project. Other supporting tools - such as a recently announced React SDK from Cloudinary - are available to provide solutions that the React core cannot.

Read more