Image Optimization Guide: Boost Your Website Speed in 5 Easy Steps

Image Optimization Guide: Boost Your Website Speed in 5 Easy Steps

Image file formats

The primary way to change the image size is to change the file format and pick the format that is best for the job. There are 4 main image formats used on the web — JPEG, PNG, SVG, GIF. Each of them has specific use cases.

No alt text provided for this image

  1. JPEGs are usually used for images with many colors such as photographs. The downside of JPEGs is that they don’t really allow for transparency. You can’t set the background of JPEG to be transparent. To have a transparent background you must use PNG or GIF. So JPEGs are for complex images with lots of colors. They also are a little bit big.
  2. GIFs usually limit the number of total colors that you can use in a GIF (2–256 colors). And reducing the color count leads to huge file savings. That’s why they are really good for animations.
  3. PNGs usually limit the number of colors you can use and because of that, they are a lot smaller in size than JPEGs. Also, you can add transparency to PNGs.
  4. SVGs are vector graphics. You can expand an SVG to several times its original size and it will be just as sharp as the original was. Also, they are very small in size. You can also customize them using CSS. But they are very simple things with a few colors.

You need to pick the right format of images and then compress them as much as you can without minimizing their quality.

When to use what?

  • for transparency use a PNG
  • for animations use a GIF
  • for colorful images use a JPG
  • for icons, logos, and illustrations use SVGs

5 simple steps to optimize your images

There are 5 main steps to minimize images

1. Reduce JPEG, and PNG size with optimizer tools (my preference is Compressor.io or TinyPNG)

Let’s optimize one large JPEG and one large PNG using TinyPNG.

Here are the original JPEG and PNG images which we need to optimize (3.0mb JPEG, 1.9mb PNG). By the way, if you don’t have a good internet speed than these images might not load for you because they are too big. That’s why we need to minimize them because not every user has perfect internet speed.

No alt text provided for this image
No alt text provided for this image
1. Large JPEG and PNG images

Let’s upload them in TinyPNG or any tool that you choose and optimize.

No alt text provided for this image

So we just saved 68% (1.3mb) for our PNG image and 46% (1.4mb) for our JPEG image.

Here are the images after optimization. And you can see that they’re still very clear for the website.

No alt text provided for this image
No alt text provided for this image
2. Large JPEG and PNG after optimization using TinyPNG

2. Resize the image based on the size it will be displayed

Match up the resolution of the images to the size that is actually displayed on a website. For example, if we’re displaying those images at a maximum of 640 pixels wide then we don’t need to keep them 5182*3454 pixels. Let’s chop them down to the actual image size. An image with half the width will be even less than half the size.

No alt text provided for this image
No alt text provided for this image
3. Large JPEG and PNG after resizing

So for the JPEG we saved another 95.6% and went from 1.6mb (5182*3454 pixels) to 71kb (640*427 pixels). And for the PNG we saved 96% and went from 600kb (6000*4000 pixels) to 24kb (640*427 pixels) .

Can you tell the difference between 1st and 3rd images?. On this size, they look as clean as they were before optimization. Overall, we saved 97.6% for our JPEG image and another 98.7% for our PNG image. Then why would we want to send ~98% extra kilobytes to our users?

3. Try to choose simple illustrations over highly detailed photographs

This one is simple. Whenever you’re building a website just think twice of what images are you using. Do you need that super detailed photo or maybe you can just use a nice icon instead?

4. Display different sized images for different backgrounds

Why would we want to send a large background image that’s for a 4k screen to somebody who’s in their mobile device? Use media queries to send different sized images for different screens.

No alt text provided for this image

The browser is smart enough to know to download whatever it actually needs. If the user uses an iPad and he or she doesn’t need a large-background image then the browser doesn’t need to load it.

5. Remove image metadata

This one is more about security. Most photos have data about where the photo was taken, what device it was taken on, etc. Use tools like exifpurge.com or verexif.com to remove metadata from the image.

No alt text provided for this image

Metadata aren’t that big (~3kb) but you should also consider removing them before you send the image to the users.

6. An alternative. Use CDNs

No alt text provided for this image

If you don’t want to do the optimizations by yourself you can just use CDNs (Content Delivery Networks) like imigix. They take care of all the images for you. Just upload all your images that you’ll be using and they will optimize and give you a URL of the optimized images which you can access.

Using all these techniques listed here we are able to save a lot of transfer that a client and a server need to do. Make sure that all your images are as small as possible so that your websites are performant.

To view or add a comment, sign in

More articles by Hayk Simonyan

  • Beginner’s Guide to Prompt Engineering with ChatGPT

    Beginner’s Guide to Prompt Engineering with ChatGPT

    Intro Prompt Engineering is one of the highest-leverage skills that you can learn in 2023. Whether you’re developing…

  • Functional Programming Simplified

    Functional Programming Simplified

    Introduction Functional Programming revolves around the principle of separating concerns, specifically the separation…

  • REST vs GraphQL

    REST vs GraphQL

    Introduction RESTful and GraphQL APIs are two popular choices for building web APIs, each with its own strengths and…

  • React Lifecycle Methods and Their Equivalents in Functional Components

    React Lifecycle Methods and Their Equivalents in Functional Components

    React is the most popular JavaScript library for building user interfaces, and it provides a set of lifecycle methods…

    1 Comment
  • Deploying a NestJS app for Free on Cyclic

    Deploying a NestJS app for Free on Cyclic

    Introduction In this article, we’re going to deploy a Nestjs app for free on Cyclic Cyclic is a cloud platform that…

    1 Comment
  • Master TypeScript Interviews

    Master TypeScript Interviews

    Intro Are you preparing for a TypeScript interview and want to know what to expect? In this article, we'll go over the…

  • 7 Design Patterns You Should Know

    7 Design Patterns You Should Know

    What are Design Patterns? Design patterns are repeatable solutions to commonly occurring problems in software design…

  • What is Dependency Injection?

    What is Dependency Injection?

    Dependency Injection (DI) is a programming design pattern that makes a class independent of its dependencies. It…

  • OOP Concepts Simplified

    OOP Concepts Simplified

    Intro In this article, we’ll look at the core OOP concepts with real code examples, which will make it easier for you…

  • Deploying Your Website to Firebase

    Deploying Your Website to Firebase

    Introduction In this article, we will deploy your website frontend to Google Firebase for FREE in less than 5 minutes…

Insights from the community

Explore topics