Use Cloudinary to optimize images
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
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.
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 .
- Press
Control+Shift+J
(orCommand+Option+J
on 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:
https://res.cloudinary.com//image/fetch//
-
<cloud_name>
is your Cloudinary cloud name.Example:
demo
-
<transformations>
is a list of Cloudinary image transformations separated by commas.Example:
q_auto,f_auto
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.Example:
https://codelab-cloudinary.glitch.me/images/flower1.png
Here's an example of a complete Cloudinary fetch URL:
https://res.cloudinary.com/demo/image/fetch/q_auto,f_auto/https://codelab-cloudinary.glitch.me/images/flower1.png
Replace image URLs with Cloudinary fetch URLs
Edit the image URL on line 25 of index.html
:
-
Create a Cloudinary fetch URL:
https://res.cloudinary.com/<cloud_name>/image/fetch/<transformations>/<remote_image_url>
-
Replace
<cloud_name>
with your Cloudinary cloud name. -
Replace
<transformations>
withq_auto,f_auto
. -
Replace
<remote_image_url>
with the original image URL.
Example:
https://res.cloudinary.com/demo/image/fetch/q_auto,f_auto/https://codelab-cloudinary.glitch.me/images/flower1.png
-
-
Replace the image URL on line 25 of
index.html
with the Cloudinary fetch URL.Example:
<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>
</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.
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>
<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>
<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>
</div>
Measure page load time after optimization
These are the results of using Cloudinary to optimize images:
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!
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.