Skip to content

Automatically Reduce Image Size Without Losing Quality

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.

Some formats such as AVIF, WebP and JPEG-2000 are more efficient than the standard JPEG format for delivering web images, but they are not supported by all browsers. 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 first image 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 second image includes f_auto in its delivery URL and so is delivered as either an AVIF (14.6 KB – a saving of 56.5%), WebP (16.1 KB – a saving of 52%), or JPEG-2000 (21.0 KB – a saving of 37%) to supported browsers.

JPEG to all browsers

With f_auto

Loading code examples

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, transforming 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 AVIF, WebP or JPEG-2000 as required by the user’s browser and delivered optimized and cached through a high-end CDN.

Loading code examples Remote fetched image with automatic format selection

The automatic format feature provides live functionality under the hood, and as browser support evolves and changes, Cloudinary customers get continuously growing value as our serving is updated.

When it comes to selecting the best format, Cloudinary does not blindly select the format only by browser, and not even by the accepts-header. Cloudinary monitors the browser ecosystem, and thanks to a vast community of users, detects issues in browser support and immediately reacts. Occasionally browser support for different formats gets broken, even if a specific browser advertises that a particular format is supported. Sometimes these failures are partial, for example broken Safari support on a specific version of an operating system, or sometimes the failures are for images with specific attributes, such as transparency, or potentially even more subtle cases.

Using f_auto means that your website media will react to browser bugs and browser updates, based on a per image analysis.

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, which 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 either an AVIF (14KB – a saving of 62%), WebP (20.2KB – a saving of 45%), or JPEG-2000 (24.3KB – a saving of 34%) to supported browsers, and JPEG (36.6KB) to all other browsers:

Loading code examples 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).

Loading code examples 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:

Loading code examples 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

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 transformation and delivery features discussed here are available with no extra charge for all Cloudinary’s plans, including the free plan.


Back to top

Featured Post