Cloudinary Blog

Optimize your JPEG images without compromising quality with JPEGmini and Cloudinary

Optimize your JPEG images without compromising quality

Image optimization is an important step to reducing page load times, improving user experience and reducing bandwidth costs. When using the JPEG image format, which is best used for photos, the most common optimization is controlling the JPEG quality level.

By lowering JPEG quality, say to 90%, 80% or even 50%, you'll get much smaller JPEG files. But will image quality be good enough? JPEG optimization is tricky because if quality is too low, you'll get blurry images, pixelation and visible compression artifacts; if it's too high, images will look good but take too long to load.

We thought it would be great to let our users automatically optimize their JPEGs without sacrificing quality. So we partnered with JPEGmini, an online service that does exactly that - it smartly balances the need to reduce file size, with the need to deliver high quality visual results. JPEGmini's JPEG optimization can reduce the file size of your photos by up to 5x, while maintaining their original quality.

We've integrated JPEGmini with our cloud-based image management solution, allowing our users to generate JPEGs on the fly with JPEGmini’s smart optimization built in. Read on to see how it's done.

Optimizing JPEG images while maintaining high image quality

Cloudinary is a cloud-based image management solution. You can upload your images to our cloud storage, perform a range of image manipulations on the fly, and then deliver your images to users via a fast CDN.

One of the image manipulations we offer is changing JPEG quality. Let's take a JPG image of a dog that was uploaded to Cloudinary, and assigned the ID 'happy_dog'.

The original image is a high-resolution photo of 2082x2975. Is was saved as high-quality JPG and weighs 2.5MB, which is quite a lot of data to deliver to your web or mobile viewers.

Cloudinary allows you to modify the quality level of a JPG by setting the quality parameter (or q if specified within the image URL) to a value between 1 to 100. In addition, further lossless optimizations are applied to the generated images. The following code, or dynamic manipulation URL, converts the original image on-the-fly to a JPEG with 90% quality (for illustration purposes, the image below was resized to a 250x358 thumbnail). The generated image weighs only 681KB, which is just 19% of the original high-quality size:

Ruby:
cl_image_tag("happy_dog.jpg", :quality=>90)
PHP:
cl_image_tag("happy_dog.jpg", array("quality"=>90))
Python:
CloudinaryImage("happy_dog.jpg").image(quality=90)
Node.js:
cloudinary.image("happy_dog.jpg", {quality: 90})
Java:
cloudinary.url().transformation(new Transformation().quality(90)).imageTag("happy_dog.jpg")
JS:
cl.imageTag('happy_dog.jpg', {quality: 90}).toHtml();
jQuery:
$.cloudinary.image("happy_dog.jpg", {quality: 90})
React:
<Image publicId="happy_dog.jpg" quality="90">
        <Transformation quality=90 />
</Image>
Angular:
<cl-image public-id="happy_dog.jpg" quality="90">
        <cl-transformation quality=90 />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Quality(90)).BuildImageTag("happy_dog.jpg")
JPEG with quality 90%

In the example above we used 90% quality, which is quite high and ensures a good visual result. However, for photos like the one above, you may be able to set a lower JPEG quality level while is indiscernible from the original to the naked eye. Using Cloudinary's new JPEGmini add-on, you can apply a JPEG optimization with the best possible quality for the specific photo you've uploaded.

Applying JPEGmini optimization is done by simply setting the quality parameter, in the code that generates the image URL, to jpegmini. The following manipulation URL and code optimizes an image uploaded to Cloudinary using JPEGmini, while delivering the result via a CDN. The resulting photo weighs only 425KB. This means we saved almost 40% of file size compared to the 90 quality JPG or more than 80% compared to the original photo. Across an entire site, this can significantly improve user experience and minimize bandwidth costs.

Ruby:
cl_image_tag("happy_dog.jpg", :quality=>"jpegmini", :sign_url=>true)
PHP:
cl_image_tag("happy_dog.jpg", array("quality"=>"jpegmini", "sign_url"=>true))
Python:
CloudinaryImage("happy_dog.jpg").image(quality="jpegmini", sign_url=True)
Node.js:
cloudinary.image("happy_dog.jpg", {quality: "jpegmini", sign_url: true})
Java:
cloudinary.url().transformation(new Transformation().quality("jpegmini")).signed(true).imageTag("happy_dog.jpg")
JS:
cl.imageTag('happy_dog.jpg', {quality: "jpegmini", sign_url: true}).toHtml();
jQuery:
$.cloudinary.image("happy_dog.jpg", {quality: "jpegmini"})
React:
<Image publicId="happy_dog.jpg" quality="jpegmini" sign_url="true">
        <Transformation quality="jpegmini" />
</Image>
Angular:
<cl-image public-id="happy_dog.jpg" quality="jpegmini" sign_url="true">
        <cl-transformation quality="jpegmini" />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Quality("jpegmini")).Signed(true).BuildImageTag("happy_dog.jpg")
JPEGMini optimized photo

As you can see, the generated photo looks identical to the original photo, which weighs so much more.

The thumbnails above were generated on-the-fly by Cloudinary, by specifying the width and height parameters - this is an easy way to automatically create thumbnails of large images for your site or mobile app.

Note that the 250x358 thumbnail optimized by JPEGmini weighs only 15.9KB, which means saving almost 50% of file size compared to a 90 quality version of the same thumbnail which weighs 30.2KB, while the results seem identical to the human eye.

Ensuring high visual quality for all images

Reducing file size, and thus improving user experience and reducing bandwidth costs is an important goal. In parallel, you'll also want to make sure that all photos are visually clear and are displayed with acceptable (or higher) quality. JPEGmini's JPEG optimization reduces JPEG quality and image size, but only up to the point at which the JPEG still looks good to the human eye. It analyzes each image and finds the quality threshold required to keep this specific image looking good, and applies that level of quality to the image.

For example, the following image was uploaded to Cloudinary with the ID of 'blue_sky'. This image is 310x207 and weighs 87.1KB.

Ruby:
cl_image_tag("blue_sky.jpg")
PHP:
cl_image_tag("blue_sky.jpg")
Python:
CloudinaryImage("blue_sky.jpg").image()
Node.js:
cloudinary.image("blue_sky.jpg")
Java:
cloudinary.url().imageTag("blue_sky.jpg")
JS:
cl.imageTag('blue_sky.jpg').toHtml();
jQuery:
$.cloudinary.image("blue_sky.jpg")
React:
<Image publicId="blue_sky.jpg" >

</Image>
Angular:
<cl-image public-id="blue_sky.jpg" >

</cl-image>
.Net:
cloudinary.Api.UrlImgUp.BuildImageTag("blue_sky.jpg")
Original blue sky photo

Trying to convert the image to a JPEG of quality 80 indeed reduces file size to 17.5KB, which saves 80% of the original file size. However, the result doesn't look good enough. The sky's gradient has some "noise", and so do the white lines and text.

Ruby:
cl_image_tag("blue_sky.jpg", :quality=>80)
PHP:
cl_image_tag("blue_sky.jpg", array("quality"=>80))
Python:
CloudinaryImage("blue_sky.jpg").image(quality=80)
Node.js:
cloudinary.image("blue_sky.jpg", {quality: 80})
Java:
cloudinary.url().transformation(new Transformation().quality(80)).imageTag("blue_sky.jpg")
JS:
cl.imageTag('blue_sky.jpg', {quality: 80}).toHtml();
jQuery:
$.cloudinary.image("blue_sky.jpg", {quality: 80})
React:
<Image publicId="blue_sky.jpg" quality="80">
        <Transformation quality=80 />
</Image>
Angular:
<cl-image public-id="blue_sky.jpg" quality="80">
        <cl-transformation quality=80 />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Quality(80)).BuildImageTag("blue_sky.jpg")
Quality 80% blue sky JPEG photo

Setting the quality parameter to jpegmini generates a better looking image with less “noise”, so you can safely embed it in your site. The image weighs 28KB, which is larger than the 80% quality version, but still saves almost 70% of the original image's size.

Ruby:
cl_image_tag("blue_sky.jpg", :quality=>"jpegmini", :sign_url=>true)
PHP:
cl_image_tag("blue_sky.jpg", array("quality"=>"jpegmini", "sign_url"=>true))
Python:
CloudinaryImage("blue_sky.jpg").image(quality="jpegmini", sign_url=True)
Node.js:
cloudinary.image("blue_sky.jpg", {quality: "jpegmini", sign_url: true})
Java:
cloudinary.url().transformation(new Transformation().quality("jpegmini")).signed(true).imageTag("blue_sky.jpg")
JS:
cl.imageTag('blue_sky.jpg', {quality: "jpegmini", sign_url: true}).toHtml();
jQuery:
$.cloudinary.image("blue_sky.jpg", {quality: "jpegmini"})
React:
<Image publicId="blue_sky.jpg" quality="jpegmini" sign_url="true">
        <Transformation quality="jpegmini" />
</Image>
Angular:
<cl-image public-id="blue_sky.jpg" quality="jpegmini" sign_url="true">
        <cl-transformation quality="jpegmini" />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation().Quality("jpegmini")).Signed(true).BuildImageTag("blue_sky.jpg")
JPEGmini optimized blue sky photo

Complex image manipulations

Cloudinary lets you apply various manipulations to your images. We've shown you a few above and many more image manipulations are supported. The JPEGmini optimization can be combined with any other image manipulations supported by Cloudinary. Here's a taste of advanced editing you can do with Cloudinary's image manipulations, combined with JPEGmini.

The following dynamic manipulation URL and sample code crops the dog's photo to a 200x230 ellipse, applies a smart photo-improving filter, adds a shadow, and finally, optimizes the result using JPEGmini for best delivery.

Ruby:
cl_image_tag("happy_dog.jpg", :sign_url=>true, :transformation=>[
  {:effect=>"improve", :gravity=>"north", :height=>230, :radius=>"max", :width=>200, :crop=>"fill"},
  {:effect=>"shadow", :quality=>"jpegmini"}
  ])
PHP:
cl_image_tag("happy_dog.jpg", array("sign_url"=>true, "transformation"=>array(
  array("effect"=>"improve", "gravity"=>"north", "height"=>230, "radius"=>"max", "width"=>200, "crop"=>"fill"),
  array("effect"=>"shadow", "quality"=>"jpegmini")
  )))
Python:
CloudinaryImage("happy_dog.jpg").image(sign_url=True, transformation=[
  {"effect": "improve", "gravity": "north", "height": 230, "radius": "max", "width": 200, "crop": "fill"},
  {"effect": "shadow", "quality": "jpegmini"}
  ])
Node.js:
cloudinary.image("happy_dog.jpg", {sign_url: true, transformation: [
  {effect: "improve", gravity: "north", height: 230, radius: "max", width: 200, crop: "fill"},
  {effect: "shadow", quality: "jpegmini"}
  ]})
Java:
cloudinary.url().transformation(new Transformation()
  .effect("improve").gravity("north").height(230).radius("max").width(200).crop("fill").chain()
  .effect("shadow").quality("jpegmini")).signed(true).imageTag("happy_dog.jpg")
JS:
cl.imageTag('happy_dog.jpg', {sign_url: true, transformation: [
  {effect: "improve", gravity: "north", height: 230, radius: "max", width: 200, crop: "fill"},
  {effect: "shadow", quality: "jpegmini"}
  ]}).toHtml();
jQuery:
$.cloudinary.image("happy_dog.jpg", {transformation: [
  {effect: "improve", gravity: "north", height: 230, radius: "max", width: 200, crop: "fill"},
  {effect: "shadow", quality: "jpegmini"}
  ]})
React:
<Image publicId="happy_dog.jpg" sign_url="true">
        <Transformation effect="improve" gravity="north" height=230 radius="max" width=200 crop="fill" />
        <Transformation effect="shadow" quality="jpegmini" />
</Image>
Angular:
<cl-image public-id="happy_dog.jpg" sign_url="true">
        <cl-transformation effect="improve" gravity="north" height=230 radius="max" width=200 crop="fill" />
        <cl-transformation effect="shadow" quality="jpegmini" />
</cl-image>
.Net:
cloudinary.Api.UrlImgUp.Transform(new Transformation()
  .Effect("improve").Gravity("north").Height(230).Radius("max").Width(200).Crop("fill").Chain()
  .Effect("shadow").Quality("jpegmini")).Signed(true).BuildImageTag("happy_dog.jpg")
Further manipulated JPEGmini optimized photo

Securing your manipulation URLs

JPEGmini is a computing-intensive operation, and you probably want to prevent unauthorized use of it. As a result, you may have noticed that all the manipulation URLs in this blog posts were signed (for example, in the last image, the URL included the secret s--J23CuGrX--). By signing URLs, you apply a layer of security, ensuring that the JPEGmini JPEG optimization can't be performed by just anyone directly typing a URL.

Another way to prevent external users from performing the JPEGmini optimization, is to “eagerly” generate optimized JPEGs while uploading your images. For more details about these options and the JPEGmini add-on in general, please refer to the documentation.

Summary

In this post we showed how JPEGmini can be used to smartly balance between JPEG compression and a high-quality visual result, and how simple it is to apply JPEGmini's optimization using Cloudinary's cloud-based image management service.

You can add the free tier of the JPEGmini add-on to your Cloudinary account and try it out on your photos using our interactive Media Library.  

As always, any feedback or comments you have would be much appreciated, either in the comments below or via Facebook or Twitter.

Recent Blog Posts

Serverless Tutorial: File Storage with Webtask and Cloudinary

Media makes up the majority of today's website content. While it makes websites more interesting for visitors, media presents challenges because these images and videos are more difficult to store, deliver and manipulate on-the-fly to suit any given situation.

Read more

ImageCon17: Delivering Responsive Images

By Jason Grigsby

After five years many specifications, some inflamed Twitter battles and other conversations, responsive images have finally landed and there's a sound. Which is really exciting right? People have been climbing for this for quite some time and we've reached a point where they're available in modern browsers. So people were excited, they wanted to go use them it's something that designers and developers have had as a point of frustration for a long time.

Read more
Auto padding images with content-aware color padding

How you present the content of your website can be just as important as the content itself. The images you display need to conform to the graphic design of your site, and every image needs to fit within a predefined size. Although that may be simple enough to achieve when you are dealing with your own images, the task can be more challenging when displaying images uploaded by your users.

Read more

Bounce! Hacking Jazzfest with Social Videos

By Eric Normand
Bounce! Hacking Jazzfest with Social Videos

Last week, I was invited to an exclusive hackathon to build apps for musicians. The app team I was assigned to was tasked with building a video upload site for Bounce videos. Bounce is a style of music that originated in New Orleans. The app would be called BounceDotCom.com and there were plans to have Big Freedia, the Queen of Bounce, promote it. I knew the organizer could make things happen, so I jumped at the chance.

Read more
Getting a Better React-ion with Progressive Web Apps

This is part 2 of a 3 part series

React has become more popular, as well as more mature, over the last four years since its release by Facebook. It has become one of the go-to technologies for people looking to componentize the front-end of any web application. It also helps that an entire mobile stack is built around React in the form of ReactNative. The components are wonderful, however there can be a burdensome learning curve. But, in the end, there’s the payoff of highly reusable code and a better user experience.

Read more
Build an Image Library with React & Cloudinary

This article was originally posted on Scotch.io

React is a good tool when it comes to building flexible and reusable UI components. However, it's "one of those libraries" that cannot handle all the tasks involved in building a full fleshed UI project. Other supporting tools - such as a recently announced React SDK from Cloudinary - are available to provide solutions that the React core cannot.

Read more