Cloudinary Blog

Compression of Image Files With Only One Line of Code

Automating the Compression of Image Files with One Line of Code

I recently came across a tweet by organizer and developer advocate iChuloo by way of @JAMStackLagos on how to reduce the size of an image with only one line of code on Cloudinary.

That tweet rang a bell. I then recalled that I’d used that exact codeline in at least two React projects in the past.

This article digs deeper, showing you how to compress image files, again with one line of code, in PHP, Java, or Python on environments or frameworks like React, Node, Vue, and Angular.

Image Compression

As a rule, compression of image files are either for storage or for delivery.

Image Storage

The compression of image files for cloud storage when requesting their upload to Cloudinary. Subsequently, Cloudinary applies to the files the transformations you specified before storing them. The examples below show the codeline in Ruby, Python, PHP, and Java. You can also code in a similar manner in other modern languages.

Ruby

Copy to clipboard
Cloudinary::Uploader.upload("cat.jpg", :quality => 60)

Python

Copy to clipboard
cloudinary.uploader.upload("cat.jpg", "quality" = "60")

PHP

Copy to clipboard
\Cloudinary\Uploader::upload("cat.jpg", [ "quality" => "60"]);

Java

Copy to clipboard
cloudinary.url().transformation(new Transformation().quality(60)).imageTag("cat.jpg");

In the code above, quality is a compression parameter that specifies the image quality and size for storage. If you’re not sure what number to assign to quality, just type auto. Cloudinary then automatically adjusts the compression quality for your image by applying the optimal balance between the image’s file size and quality.

Image Delivery

In this context, you upload images straight to the cloud and then apply the quality compression parameter when delivering them to users. You can also serve images in the formats that pertain to the various web browsers and mobile devices.

You configure quality on a 0-100 scale. The higher the image quality, the larger the image size; the lower the image quality, the smaller the image size. To compress images on the fly, adjust their quality parameter. See the examples below.

Node.js

Copy to clipboard
cloudinary.image("cat", {quality: 50})

Java

Copy to clipboard
cloudinary.url().transformation(new Transformation().quality(50)).imageTag("cat");

Python

Copy to clipboard
CloudinaryImage("dog").image(quality=cat)

Alternatively, simply add a q parameter to your image’s URL, for example:

Copy to clipboard
https://res.cloudinary.com/demo/image/upload/q_50/cat.jpg

Here, q stands for quality; 50 is your choice on a 0-100 scale.


Front-end developers can drop the components in their app out of the box, like this:

React.js

Copy to clipboard
<Image publicId="cat.jpg" ><Transformation quality="50" /></Image>

Vue.js

Copy to clipboard
<cld-image publicId="cat.jpg" ><cld-transformation quality="50" /></cld-image>

Angular

Copy to clipboard
<cl-image public-id="cat.jpg" ><cl-transformation quality="50"></cl-transformation></cl-image>

JQuery

Copy to clipboard
$.cloudinary.image("cat.jpg", {quality: 50})

JavaScript

Copy to clipboard
cloudinary.imageTag('cat.jpg', {quality: 50}).toHtml();

Here, cat is the name of the uploaded image, which is usually the public ID (publicId) on the Cloudinary storage platform.

Conclusion

Uploading without images optimization or compression wastes bandwidth. Plus, delivering them on your app invariably takes longer. Why not automate the compression process with Cloudinary’s effective and simple drop-in tools so that your visuals load fast?

Register for free and for more details, check out the following Cloudinary documentation:


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