Skip to content

Where to Start Optimizing My Images

Published:

This post was originally published on Octuweb in October 2019.

As web development professionals, we worry about choosing the best framework, the most current library, applying best practices in JavaScript and CSS, being careful with HTML semantics to improve SEO, improving our content’s accessibility, bundling and splitting files to optimize loading, but what about images?

Table of Contents

Open Table of Contents

Images on the Web

Images are a very important part of the web. Surely with the constant advancement of technology, where we see continuous improvement in connectivity, possibly in the future content will have more videos than images. But the foundation will remain the same, improving user experience, which is what our work is really about.

According to HTTP Archive, 50% of transferred data to load a web page is images (JPEG, PNG and GIF). In August 2019, on mobile, images represented 911KB of loaded content for the average 1.8MB site, and on desktop it’s 1MB images in an average of 2MB.

HTTP Archive: Table

Image Optimization Image eXperience is Everyone’s Business

A few days ago, in the article Web Performance in Modern Times, Miguel Ángel told us about a situation where he had problems planning a weekend activity: “Where I should see schedules, white appears on white. I think it’s loading, I’m not sure either… the train has arrived and my phone goes into my pocket. ‘I’ll try the other cinema’s website later’…”

It’s very likely that the white text for the schedules is over a fantastic and incredible image of a key scene from the latest film release, perfectly contrasted, perfectly framed to catch the viewer’s attention and inform them of showtimes to watch that great film… but, the image hasn’t loaded.

This is where I allow myself to use the term IX (Image eXperience). There are several points to consider for good IX, and not just running images through a compressor that promises to optimize our images most effectively.

In web development there are various profiles, whether in teams, individuals, or someone who plays more than one of these roles: product, UX, design, development, QA, even DevOps (or what’s now known as SRE) in the production deployment process. Each of these roles has the responsibility to offer the best for the user, provide maximum value to the product, pay attention to details.

Let’s continue with the example of the cinema schedule page that Miguel Ángel couldn’t see. In that situation, these are the questions I ask myself that could have been used as a checklist in the ideation, design, development, and deployment process of that page:

As you can see, image optimization for the web isn’t just choosing what compression percentage to save the image at. There are many things to consider and many profiles that can intervene and detect a possible failure in such a critical part of the product as in the example.

Treating all these points in depth would fill a book… oops!, I’m already working on it 🤪

Optimize Adventure Time

In the scenario we find ourselves in, we’ve already done the preliminary analysis of image needs. As the article title indicates, the most common question is Where do I start to optimize my images?.

Optimization from Graphic Editors

In the first line of action in image optimization we have image editors, like Photoshop, Affinity Photo, Pixelmator, PaintShopPro, Paint.net or GIMP among others. Let’s not forget Sketch which, although not an image editing program, is widely used in interface editing(1).

Photoshop

It’s the most used image editor by design teams. Curiously not the one that offers us the most optimization options. In its latest version CC 2019, it offers an export dialog window with some improvements to generate images of various densities, as can be seen in the left column, but doesn’t leave us much choice regarding image format and configuration parameters.

Photoshop: Export

We can export to PNG, JPG, GIF and SVG formats (yes, SVG, it generates an SVG file with the image embedded in base64), beyond the quality percentage(2) we don’t have many configuration parameters to export images. What’s very striking is that we can’t count on WebP format for export.

In previous versions of Photoshop, and in this one under the name “Save for Web… (legacy)”, it offers us some predefined configurations, always according to Adobe team criteria. One of the parameters I miss in Photoshop is Chroma subsampling(3) where we could better optimize according to our criteria. Instead they maintain 4:4:4 (1x1) subsampling between 51% to 100% quality percentage values and below 51% they apply 4:2:0 (2x2) subsampling. That’s why you’ll notice a notable change between an image exported at 51% and the same image at 50%.

Affinity Photo

It’s another image editor that’s gaining traction in the design community. So let’s see what it offers us for image export.

Affinity Photo: Export

We can see that in export they offer us more formats to select(4), but let’s not be fooled, compatible for web we still have PNG, JPEG, GIF and SVG available. But like Photoshop, it maintains simplicity in compression parameters with a slider to select values between 1% and 100%.

Pixelmator

A very interesting and economical alternative for image editing. It doesn’t improve on the compression quality selection option of previous editors, but we can see they’ve added WebP format as an option to improve our image optimization. Even with WebP, we can select the quality percentage(5).

Pixelmator: Export

GIMP

It’s possibly the least used tool in the design world. It’s an Open Source tool, with a large community behind it, and that can be seen in the numerous configuration parameters we have available in the advanced options when exporting an image.

GIMP: Export

Where we see we can configure more advanced parameters like Smoothing, Restart markers, Subsampling or DCT Method(6). These are configuration parameters that can be scary, and even make us invest a lot of time doing tests.

Other Editors

There are many editors, we can’t analyze them all. Most coincide with the functionality of defining a value scale for optimization of images with quality loss, like JPG and some with the option to do it also with WebP.

Optimization from Applications

In the previous section we analyzed the tools usually used by design teams for image editing. That team can extend the optimization boundary by using desktop application format tools, specific for this purpose.

Wait, we said that IX is everyone’s business. So here we have the opportunity to know tools that all roles involved in product development can use… let’s get to know some of these applications.

If you’re in a Mac environment, the best option (IMHO) is ImageOptim(7) by Kornel Lesiński and it’s also free.

ImageOptim

Like most desktop applications in Mac environments, one of the characteristics that makes it stand out is a simple appearance, but don’t let that fool you, it’s a very powerful and professional tool.

By just dragging an image, a group of them or a folder, the application starts working, optimizing all images. Keep in mind that what it does is replace the original image with the optimized one. That’s a topic I’d like to comment on, but we’ll see it in the next section.

The default configuration offers lossless image compression, that means we don’t notice visual changes, but we will reduce the size. We have the option to check the Enable lossy minification checkbox to select quality percentages for lossy optimization.

ImageOptim: Quality

One of the things I like most about ImageOptim is that behind that simplicity it hides a list of compressors(8) to get the best results in each case. It runs each of the compressors we have checked in the list on each of the images and selects the image with the best result. This process takes time, but I can assure you it’s worth it.

ImageOptim: Compressors

There are ImageOptim alternatives for Windows and Linux as we can see on its own website.

I’ll leave you references to other applications (for Mac) that you can try, many of them paid with trial versions for a few days.

Image Optimization Apps

Of these alternatives, the one I liked is Optimage, on the web they promise it’s more effective(9) than ImageOptim. It’s a more complete application, as it allows us to change the image format, change its dimensions, we can configure output folder or add prefix/suffix to not overwrite original images. JPEGmini Pro is very focused on the photography world, and really achieves very impressive results.

Optimization from Local CLI Tools

For people with more technical interests, wanting to control configuration based on parameters and be able to automate image optimization, we also have interesting options. We can install and configure native compressors(10), the vast majority in C, as is the case with MozJPEG or WebP (here we have it precompiled).

MozJPEG CLI

Example CLI command to compress an image with MozJPEG

WebP CLI

Example CLI command to convert a JPEG image to WebP

We also have npm packages that offer us a wrapper with a much simpler API that we can integrate into our JavaScript application workflow.

There are several, one very interesting is next-optimized-images with options that go far beyond compressing images, as it contains other packages as dependencies that offer us the possibility of generating LQIP(11) images with lqip-loader or responsive-loader which generates the different sizes of responsive images we need on our site. For image optimization it uses several imagemin packages, so let’s take a look at it.

imagemin

Upon entering imagemin what we can see is that it’s not a GitHub repository, but an organization with all repositories (37) related to this ecosystem.

It’s a very modular system, the base imagemin package has 3 encoders defined as dependencies (JPEG, SVG and WebP), but we can add all the ones we need.

We’ll see there are a couple of patterns in the repository names of this organization:

This approach makes it a very versatile solution, as modules can be maintained, updated and upgraded separately.

It can be used both as CLI (Command Line Interface) with imagemin-cli and from JavaScript. Here you’ll find implementation examples in npm, Gulp and Webpack from exercises of a course I taught at Escuela IT. In these examples you can see that I always keep the original image, to always be able to compress from the original image and not re-compress an image that has already been compressed, and sometimes by another compressor.

It’s not a good idea to do that. In this video you can see an example of what happens when re-compressing the same image:

The format with best results is FLIF(12), but it’s a format not supported by current browsers, so MozJPEG could be a viable option. But as I mentioned, I prefer to preserve the original image, in PNG if possible, to preserve all information and generate from it the compressed image and/or format change.

Imagemin is the most comfortable option if what we want is to automate image compression in the production deployment process. Both for the number of plugins and for the ease of integrating them into our stack.

One thing to keep in mind is the time it can take us to find the correct parameters to achieve the best result, since each imagemin plugin has its own value scale for optimization (sound familiar, the same happens in image editors).

Image Optimization with Third-Party Online Services

Another option we have available to optimize our images is to use an online service like Cloudinary, imgix, Fastly’s Image Optimizer, ImageOptim API or Kraken. These services offer us much more than optimizing, changing image size or format, they also offer cache service and CDN, which will benefit us if our product is visited from different parts of the world.

Each of them has its own functionalities, advantages, configurations and integrations, but I as a good Cloudinary Ambassador am going to talk to you about Cloudinary 😊

To know the functionalities that Cloudinary has you can consult the website. I want to talk to you about the functionalities I like, those I believe provide value, both for us as demanding developers and for IX.

Let’s start by saying that in the plans it offers, it has a free one with which we can try the tool without many limitations. We can use Cloudinary as an image server for a blog like this without problems.

Image Optimization

We can optimize both images we upload to Media Library of our account with the /upload/ method, and do it with remote images, with the /fetch/ method, documentation(13)

Cloudinary methods

For the example we’ll use the API via URL:

Cloudinary f_auto

Between the /upload/ type and the image version /v1570203279/, we have transformation methods. In this case I put f_auto, which refers to automatic format(14). This parameter will deliver the image in the best possible format according to the client’s request. If our client supports WebP it will deliver WebP, if our client supports JPEG 2000 as is the case with Safari, regardless of the original image format.

Let’s see an example of this.

Cloudinary: Chrome vs Firefox vs Safari

In the image we see an example of one of the images from Cloudinary’s website, in this case homepage-XX.png, there are requests for different Media Query sizes because I forced it. The original image is in PNG format and we can see that although the HTML request is for .png, Cloudinary delivers a WebP image for Chrome and Firefox, and in Safari’s case delivers a JPEG 2000 format. JPEG 2000 format has a larger size than WebP, but currently Safari doesn’t support that image format, instead it receives a JPEG 2000 image that on one hand it can render and that’s more compressed than JPEG and the original PNG.

Responsive Images

Image optimization isn’t just about compression and image quality, we also have to consider delivering them at the correct size, according to device, pixel density(15) and media queries. Here Cloudinary pleasantly surprises us again with Responsive Breakpoints Generator, a tool to generate all the images and code we need to cover the media queries we need.

Responsive Breakpoints Results

Implementation, Scalability and Integration

Another thing I like about Cloudinary is the great support they offer for different SDKs for implementation: HTTPS/HTTP, Ruby, PHP, Python, Node.js, Java, JS, jQuery, React, Angular, .Net, Android and iOS. Plus we have them available Open Source.

I refer to scalability because of the Add-ons philosophy they have. We can activate (these are paid) services for Auto-Tagging with Machine Learning, remove image backgrounds with Cloudinary AI Background Removal or apply style transfer with Neural Artwork Style Transfer.

As for integration, it’s very interesting to see that we can integrate with CMS-eCommerce integrations Salesforce Commerce Cloud, Magento, Wordpress or Zapier.

This Doesn’t End Here

There are many functionalities, Add-ons, plugins, tools, so that you can have a simple and visual guide to Cloudinary’s highlights, here I leave you a website where you can play with the API and some of the photos they took of me at JS Camp 2019, CloudiCam.dev

One More Thing

Other Online Services

There are many online services to compress images, generally they compress images one by one, and not in batch like desktop applications do. I just want to comment on a couple of them that seem interesting to me:

Oops! Don’t Forget to Optimize Any Image

It’s very easy for day-to-day work to make us fall (unconsciously) into the error of not optimizing our images, but we have the option to integrate one of the following tools.

ImgBot

A service to optimize images based on Magick.NET (a .NET library for ImageMagick) that we can integrate with our GitHub repository. We can configure it to indicate how often it will “parse” our images, define folders, images or extensions to ignore, as well as compression level. Most interesting is that it generates a Pull Request with the suggestion of optimized images.

ImgBot: Pull Request

image-actions

With the newly arrived GitHub Actions, the folks at Calibre have worked on a solution similar to ImgBot. The philosophy is the same, generate a Pull Request with optimized images.

image-actions: Pull Request

At the Blacksmith’s House…

All images in this article have been optimized with ImageOptim, most in PNG format as they’re screenshots with text. PNG is the ideal format to maintain quality and not generate noise around texts.

ImageOptim: list images

List of images optimized with ImageOptim(16)

With everything learned, if Félix ran all these images through Cloudinary they would be more optimized and with a format adapted to the browser used by the user. That’s what we’ve talked about with IX 😉

Resources

Image and Video Optimization Course

Finally, I want to share with you that I’m working on an online course on Image and Video Optimization.

Image and Video Optimization Course

Notes


Previous Post
Motion Craftsmanship