Cloudinary Blog

Compress Images for Web and Boost Performance on Your Site

Compress Images for Web and Boost Performance on Your Site

Slow-loading web content and problematic media displays that involve seemingly interminable scrolling tick off users to no end. Compressing online images is, without question, a critical task for spearheading customer retention for websites. Keep in mind that small images can still look sharp. This article shows you how to achieve that by mastering the techniques of compressing images for the web.

Image-Compression Techniques

To compress images and, consequently, reduce their sizes, you apply a transformation technique that removes the image parts with a negligible or low visual effect on the display quality. Image compression can be lossless or lossy.

Lossless Compression

Lossless compression of images results in no loss of pixel data, significantly reducing their sizes with no quality compromise in the display. The examples below demonstrate lossless image-compression in play.

Here’s the original, uncompressed picture with a size of 2.58 MB:

Ruby:
Copy to clipboard
cl_image_tag("eden_group.jpg")
PHP v1:
Copy to clipboard
cl_image_tag("eden_group.jpg")
PHP v2:
Copy to clipboard
(new ImageTag('eden_group.jpg'));
Python:
Copy to clipboard
CloudinaryImage("eden_group.jpg").image()
Node.js:
Copy to clipboard
cloudinary.image("eden_group.jpg")
Java:
Copy to clipboard
cloudinary.url().imageTag("eden_group.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('eden_group.jpg').toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("eden_group.jpg")
React:
Copy to clipboard
<Image publicId="eden_group.jpg" >

</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="eden_group.jpg" >

</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="eden_group.jpg" >

</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.BuildImageTag("eden_group.jpg")
Android:
Copy to clipboard
MediaManager.get().url().generate("eden_group.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().generate("eden_group.jpg")!, cloudinary: cloudinary)
Eden friends

Here’s the compressed version with a size of 918 KB:

Ruby:
Copy to clipboard
cl_image_tag("eden_group.jpg", :quality=>"auto")
PHP v1:
Copy to clipboard
cl_image_tag("eden_group.jpg", array("quality"=>"auto"))
PHP v2:
Copy to clipboard
(new ImageTag('eden_group.jpg'))
  ->delivery(Delivery::quality(Quality::auto()));
Python:
Copy to clipboard
CloudinaryImage("eden_group.jpg").image(quality="auto")
Node.js:
Copy to clipboard
cloudinary.image("eden_group.jpg", {quality: "auto"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().quality("auto")).imageTag("eden_group.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('eden_group.jpg', {quality: "auto"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("eden_group.jpg", {quality: "auto"})
React:
Copy to clipboard
<Image publicId="eden_group.jpg" >
  <Transformation quality="auto" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="eden_group.jpg" >
  <cld-transformation quality="auto" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="eden_group.jpg" >
  <cl-transformation quality="auto">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Quality("auto")).BuildImageTag("eden_group.jpg")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().quality("auto")).generate("eden_group.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setQuality("auto")).generate("eden_group.jpg")!, cloudinary: cloudinary)
Eden friends

The two pictures look the same even though the second one has undergone compression that reduced its size by almost 2 MB.

Lossy Compression

As its name implies, lossy compression causes loss of pixel data. That’s a result of the removal of certain image parts.

Lossless Compression Versus Lossy Compression

Before deciding which compression technique to adopt for your images, familiarize yourself with the results that the two techniques produce.

Lossless Compression

Applying lossless compression to images generates the following:

  • A lossless attribute for images that are of the GIF, BMP, RAW, or PNG file type.
  • An outstanding display.
  • Possibly less reduction in file size than that engendered through lossy compression.

Lossy Compression

Applying lossy compression to images generates the following:

  • A lossy attribute for images that are of the JPEG or GIF file type.
  • A greatly reduced file size.
  • Irreversible images: once you go lossy, you’re beyond the point of no return.
  • Possible degradation of image quality in case of application of extreme parameters.

Benefits of Image Compression

Compressing online images yields remarkable benefits.

Fast Page-Loading Speed

Large media files can hamper page-loading speed, hence potentially a huge bottleneck for content access. Compressing those files before or immediately after upload significantly improves user experience for websites.

Cloudinary, a stellar software-as-a-service (SaaS) platform for storing, managing, transforming, and delivering digital media, offers a free, eye-opening tool for analyzing a site’s loading experience. Have a whirl!

Faster Backups

Regular periodic backups are key for data integrity. As a matter of course, backing up smaller image files takes less time and less resources.

Lower Bandwidth

Loading smaller files takes less bandwidth on the audience’s part, ensuring satisfaction and appreciation.

Less Storage Space

Even though data-storage options abound in the face of technology advances, be vigilant about economizing storage space at all times. Why? Because data racks up pretty fast over time, especially if your site contains media-heavy user-generated content.

Whether you’re leveraging third-party cloud storage or hosting it yourself, it’s a good practice to compress media files before storing them. Space optimization is paramount..

Optimized Search

How does image compression benefit SEO? The answer is simple: speed is an ever-present criterion for ranking websites at search giants like Google.

The faster a webpage loads, the higher it ranks. Well-compressed images make your website load fast, which in turn raises the probability of a higher ranking on search matches.

Image-Compression Tools

The following articles I wrote in 2017 contain recommendations for image-compression tools for three development environments:

  • For PHP, Imagick works well. Built-in functions in PHP also compressing images. See the details in this article.
  • For WordPress, this article suggests several tools, from Imagify to Optimuss, with many amazing options.
  • For Ruby, read my take in this article.
  • For Python Image Optimization and Transformation, read more here

Image Compression With One Line of Code With Cloudinary

In addition, while uploading images to Cloudinary, you can efficiently automate image compression by adding —one line of code to the URL that contains the quality parameter according to the syntax q_, followed by the quality level on a scale of 0–100. For example::

Ruby:
Copy to clipboard
cl_image_tag("dog.jpg", :quality=>50)
PHP v1:
Copy to clipboard
cl_image_tag("dog.jpg", array("quality"=>50))
PHP v2:
Copy to clipboard
(new ImageTag('dog.jpg'))
  ->delivery(Delivery::quality(50));
Python:
Copy to clipboard
CloudinaryImage("dog.jpg").image(quality=50)
Node.js:
Copy to clipboard
cloudinary.image("dog.jpg", {quality: 50})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().quality(50)).imageTag("dog.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('dog.jpg', {quality: 50}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("dog.jpg", {quality: 50})
React:
Copy to clipboard
<Image publicId="dog.jpg" >
  <Transformation quality="50" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="dog.jpg" >
  <cld-transformation quality="50" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="dog.jpg" >
  <cl-transformation quality="50">
  </cl-transformation>
</cl-image>
.NET:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Quality(50)).BuildImageTag("dog.jpg")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().quality(50)).generate("dog.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setQuality(50)).generate("dog.jpg")!, cloudinary: cloudinary)
sample

The higher the image quality, the larger the file size.

This recent post tells you the details. Do check it out.

Tip: To apply lossy compression to animated GIFs, follow the procedure described in this post.

Conclusion

Compressing images goes a long way toward making the web run faster. And the procedure involved is a breeze. Let’s swear off sending uncompressed images to our audience!


Further Reading on Image Optimization

Recent Blog Posts

Our $2B Valuation

By
Blackstone Growth Invests in Cloudinary

When we started our journey in 2012, we were looking to improve our lives as developers by making it easier for us to handle the arduous tasks of handling images and videos in our code. That initial line of developer code has evolved into a full suite of media experience solutions driven by a mission that gradually revealed itself over the course of the past 10 years: help companies unleash the full potential of their media to create the most engaging visual experiences.

Read more
Direct-to-Consumer E-Commerce Requires Compelling Visual Experiences

When brands like you adopt a direct–to-consumer (DTC) e-commerce approach with no involvement of retailers or marketplaces, you gain direct and timely insight into evolving shopping behaviors. Accordingly, you can accommodate shoppers’ preferences by continually adjusting your product offering and interspersing the shopping journey with moments of excitement and intrigue. Opportunities abound for you to cultivate engaging customer relationships.

Read more
Automatically Translating Videos for an International Audience

No matter your business focus—public service, B2B integration, recruitment—multimedia, in particular video, is remarkably effective in communicating with the audience. Before, making video accessible to diverse viewers involved tasks galore, such as eliciting the service of production studios to manually dub, transcribe, and add subtitles. Those operations were costly and slow, especially for globally destined content.

Read more
Cloudinary Helps Minted Manage Its Image-Generation Pipeline at Scale

Shoppers return time and again to Minted’s global online community of independent artists and designers because they know they can count on unique, statement-making products of the highest quality there. Concurrently, the visual imagery on Minted.com must do justice to the designs into which the creators have poured their hearts and souls. For Minted’s VP of Engineering David Lien, “Because we are a premium brand, we need to ensure that every single one of our product images matches the selected configuration exactly. For example, if you pick an 18x24 art print on blue canvas, we will show that exact combination on the hero images in the PDF.”

Read more
Highlights on ImageCon 2021 and a Preview of ImageCon 2022

New year, same trend! Visual media will continue to play a monumental role in driving online conversions. To keep up with visual-experience trends and best practices, Cloudinary holds an annual conference called ImageCon, a one-of-a-kind event that helps attendees create the most engaging visual experiences possible.

Read more