Use Cloudinary to optimize images
This codelab uses Chrome DevTools. Download Chrome if you don't already have it.
Learn to use Cloudinary's fetch URL API to optimize images and serve them in next-gen formats.
In this codelab:
- Get a free Cloudinary account.
- Measure page load time before optimization.
- Optimize images with Cloudinary.
- Measure page load time after optimization.
Get 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.
Measure page load time before optimization
Use Lighthouse to measure page load time before optimization:
- Click Remix to Edit to make the project editable.
- To preview the site, press View App. Then press Fullscreen .
Command+Option+Jon Mac) to open DevTools.
- Click the Audits tab.
- Select the Performance checkbox.
- Click Run audits.
When the audit completes, look at the Opportunities section:
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:
<cloud_name>is your Cloudinary cloud name.
<transformations>is a list of Cloudinary image transformations separated by commas.
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
Create a Cloudinary fetch URL:
<cloud_name>with your Cloudinary cloud name.
<remote_image_url>with the original image URL.
Replace the image URL on line 25 of
index.htmlwith the Cloudinary fetch URL.
<div class="price">Violet bouquet- $9</div>
This decreases image size by more than 90%.
The photo on the right is 92.39% smaller than the one on the left, yet would probably look identical to the average user.
Now prepend all image links in the
https://res.cloudinary.com/demo/image/fetch/q_auto,f_auto/. Make sure to change
demo to your
The edited part of your
index.html file should now look like this:
<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>
<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>
<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:
|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!
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.