Learn Measure Blog About

Use Cloudinary to optimize images

Ahmad Awais
Ahmad Awais

Learn to use Cloudinary's fetch URL API to optimize images and serve them in next-gen formats.

In this codelab:

  1. Get a free Cloudinary account.
  2. Measure page load time before optimization.
  3. Optimize images with Cloudinary.
  4. Measure page load time after optimization.

Get a free Cloudinary account

First, sign up for a free Cloudinary account.

Remember to set a custom cloud name at the end of the registration form. Cloudinary uses your cloud name to build the URLs it will serve your images from.

Setting a custom cloud name.

Measure page load time before optimization

Use Lighthouse to measure page load time before optimization:

  1. Click Remix to Edit to make the project editable.
  2. To preview the site, press View App. Then press Fullscreen fullscreen.
  3. Press Control+Shift+J (or Command+Option+J on Mac) to open DevTools.
  4. Click the Audits tab.
  5. Select the Performance checkbox.
  6. Click Run audits.

When the audit completes, look at the Opportunities section:

Example audit results before optimization
Before optimization: Example results of a Lighthouse performance audit.

You'll resolve these opportunities in the rest of this codelab by optimizing the images with Cloudinary.

Optimize images with Cloudinary

To optimize the three images in the starting code, you'll create Cloudinary fetch URLs for each one.

A Cloudinary fetch URL looks like this:


q specifies image quality that Cloudinary will deliver. q_1 is the lowest quality; q_100 is the highest. q_auto tells Cloudinary to calculate the optimal image quality automatically.

f specifies image format. Cloudinary can deliver images in WebP and JPEG-XR formats on supported browsers. f_auto tells Cloudinary to choose an image format automatically.

  • <remote_image_url> is the original image URL.



Here's an example of a complete Cloudinary fetch URL:


Replace image URLs with Cloudinary fetch URLs

Edit the image URL on line 25 of index.html:

  1. Create a Cloudinary fetch URL:

    • Replace <cloud_name> with your Cloudinary cloud name.

    • Replace <transformations> with q_auto,f_auto.

    • Replace <remote_image_url> with the original image URL.


  2. Replace the image URL on line 25 of index.html with the Cloudinary fetch URL.


    <div class="wrapper">
    <img src="https://codelab-cloudinary.glitch.me/images/flower1.png"/>
    <img src="https://res.cloudinary.com/demo/image/fetch/q_auto,f_auto/https://codelab-cloudinary.glitch.me/images/flower1.png"/>
    <div class="price">Violet bouquet- $9</div>

This decreases image size by more than 90%.

289 KB (Original)

22 KB (Cloudinary)

The photo on the right is 92.39% smaller than the one on the left, yet would probably look identical to the average user.

Objective: Now prepend all image links in the index.html with https://res.cloudinary.com/demo/image/fetch/q_auto,f_auto/. Make sure to change demo to your cloud_name.

✔︎ Check-in

The edited part of your index.html file should now look like this:

<div class="wrapper">
<img src="https://res.cloudinary.com/demo/image/fetch/q_auto,f_auto/https://codelab-cloudinary.glitch.me/images/flower1.png" alt="Yellow bouquet" />
<div class="price">Yellow bouquet - $9</div>
<div class="wrapper">
<img src="https://res.cloudinary.com/demo/image/fetch/q_auto,f_auto/https://codelab-cloudinary.glitch.me/images/flower2.jpg" alt="Cream bouquet" />
<div class="price">Cream bouquet - $5</div>
<div class="wrapper">
<img src="https://res.cloudinary.com/demo/image/fetch/q_auto,f_auto/https://codelab-cloudinary.glitch.me/images/flower3.png" alt="Light pink" />
<div class="price">Light pink bouquet - $6</div>

Measure page load time after optimization

These are the results of using Cloudinary to optimize images:

Before and after: Image size comparison
Image Original Cloudinary Size
flower1.png 289 KB 22 KB ↓ -92.39%
flower2.jpg 59 KB 19 KB ↓ -67.8%
flower3.png 367 KB 38 KB ↓ -89.65%

Run the Lighthouse performance audit again to see for yourself!

Lighthouse performance audit: Before
Lighthouse performance audit: Before
Lighthouse performance audit: After
Lighthouse performance audit: After

The Lighthouse performance audit should have a perfect score. In the example above, First Meaningful Paint is down by an impressive 2.5 seconds.

Success: You have used Cloudinary to compress the images optimally, and your page is serving next-gen image formats.

Further Reading