Improving page speed: optimizing images
Tags: webdev
Published: 2020-03-19 11:49:49

---
title: Improving page speed: optimizing images
published: true
date: 2018-07-12 19:00:03 UTC
tags: webdev
canonical_url: https://codingfix.com/improving-page-speed-optimizing-images/?utm_source=rss&utm_medium=rss&utm_campaign=improving-page-speed-optimizing-images
---

Optimizing images for your web page images is a very important and strategically crucial task to improve the average page speed. Image size affects your page speed in several ways and we have to take care of different techniques to optimize page.
<h2>Resize your images</h2>
First, we have to take care about images' size, that is their width and height. If you're lazy, like I <a href="http://codingfix.com/wp-content/uploads/2018/07/respic.png"><img class="size-full wp-image-927 alignright" src="http://codingfix.com/wp-content/uploads/2018/07/respic.png" alt="Resize pictures" width="300" height="250" /></a>am ;), you're likely to let css do the job for you: you just upload your image and then you write a bunch of css rules to resize it (or maybe you still even set width and height as attributes of the &lt;img&gt; tag!!!). This is a huge mistake: hundreds of kilobytes can be saved serving scaled images. A tool like <a href="https://gtmetrix.com">GTMetrix</a> will sign this as a high priority task to improve page speed.

So if the image that is really displayed in the web page has a width of 300 px and a height of 120 px, don't use your original image by 1200 px x 480 px: resize it instead and use the one of the requested size. If the same image must be displayed in different sizes, upload to your server one image per each size, maybe appending a suffix which specifies width and height to the image name. And be sure to link the right image to the 'src' attribute of your &lt;img&gt; tags.

The golden rule here is: <strong>don't use CSS to resize your images!</strong>
<h2>Combine your images</h2>
Another useful technique to use in order to optimize the loading time of our images is to use sprites. This technique has born in video games development to create animations and it can be proficiently be used in web development too.

Let's say you're building a multi-language website. Probably you'll want to put somewhere in your page 2, 3 or more flags the user can click on to change language accordingly. The best way to do it is to build a unique image which holds all your flags you want to use and use css to make visible only one flag per time. Let's do it with an example.

Suppose to have 3 flags images like these ones:
<table style="border-collapse: collapse; width: 100%;" border="1">
<tbody>
<tr>
<td style="width: 33.3333%;"><a href="http://codingfix.com/wp-content/uploads/2018/07/flag_en.png"><img class="aligncenter size-full wp-image-929" src="http://codingfix.com/wp-content/uploads/2018/07/flag_en.png" alt="Union Jack" width="32" height="32" /></a></td>
<td style="width: 33.3333%;"><a href="http://codingfix.com/wp-content/uploads/2018/07/flag_es.png"><img class="aligncenter size-full wp-image-930" src="http://codingfix.com/wp-content/uploads/2018/07/flag_es.png" alt="Rojigualda" width="32" height="32" /></a></td>
<td style="width: 33.3333%;"><a href="http://codingfix.com/wp-content/uploads/2018/07/flag_it.png"><img class="aligncenter size-full wp-image-931" src="http://codingfix.com/wp-content/uploads/2018/07/flag_it.png" alt="Tricolore" width="32" height="32" /></a></td>
</tr>
</tbody>
</table>
<h3>The wrong way</h3>
We could be tempted to write something like this:
```html
<ul id="flags">
    <li>
        <a href="#" class="lang active" id="es">
            <img src="images/flags/flag_es.png" width="32" height="32" />
        </a>
    </li>
    <li>
        <a href="#" class="lang" id="en">
            <img src="images/flags/flag_en.png" width="32" height="32" />
        </a>
    </li>
    <li>
        <a href="#" class="lang" id="it">
            <img src="images/flags/flag_it.png" width="32" height="32" />
        </a>
    </li>
</ul>
```
<h3>The right way</h3>
Instead to use the three images separately nested in the three &lt;anchor&gt; tags which presumably will trigger the language change event, we're going to use our image editor of choice to combine the three flags in only one image file. Each flag is 32 px x 32 px so we can build an image with a width of 32 px and a height of 96 px. We'll get something like this:
<p style="text-align: center;"><a class="bordered" href="http://codingfix.com/wp-content/uploads/2018/07/flags.png"><img class="aligncenter wp-image-932 size-full" src="http://codingfix.com/wp-content/uploads/2018/07/flags.png" alt="Flags sprite" width="32" height="96" /></a></p>
Now we can use the following html structure for our language menu:
```html
<ul id="flags">
    <li>
        <a href="#" class="lang active" id="es"&gt;&lt;/a>
    </li>
    <li>
        <a href="#" class="lang" id="en"&gt;&lt;/a>
    </li>
    <li>
        <a href="#" class="lang" id="it"&gt;&lt;/a>;
    </li>
</ul>
```
Below you find the CSS rules for this piece of markup:
```css
ul#flags{
    list-style-type: none !important;
    margin-bottom: 0 !important;
}
ul#flags li{
    display: inline !important;
}
ul#flags li a{
    display: inline-block !important;
    width: 30px !important;
    border-radius: 50% !important;
    overflow: hidden !important;
    text-align: center !important;
}
ul#flags li a#es{
    background: url(../images/flags/flags.png) 0 0 no-repeat;
}
ul#flags li a#en{
    background: url(../images/flags/flags.png) 0 32px no-repeat;
}
ul#flags li a#it{
    background: url(../images/flags/flags.png) 0 64px no-repeat;
}
```
Do you notice the last three rules? There we set the top property of the background image: 0 for the Spanish flag, 32 for English one and 64 for the Italian one. The result will look like this:
<p style="text-align: center;"><a href="http://codingfix.com/wp-content/uploads/2018/07/flags-1.png"><img class="aligncenter size-full wp-image-934" src="http://codingfix.com/wp-content/uploads/2018/07/flags-1.png" alt="Language Flags" width="111" height="44" /></a></p>
In this example, I have highlighted the active language with an orange circle (using the javascript to change the active item on click event, but I'll let this for another article about how to create a multi-language web pages using a couple of fantastic plugins for jQuery).

What is the advantage of using sprites? As GTMetrix explains,
<blockquote>Combining images into as few files as possible using CSS sprites reduces the number of round-trips and delays in downloading other resources, reduces request overhead, and can reduce the total number of bytes downloaded by a web page.</blockquote>
<h2>Compress your images</h2>
<h3>Option 1</h3>
Be whatever it be the techniques you're using to optimize your images, it's almost sure that PageSpeed Insights (or GTMetrix or whatever other tool you're using to measure your page performances) will tell you "<strong>Optimize images</strong>". The good news is that both GTMetrix and PageSpeed Insights allow you to download optimized images from their servers. PageSpeed Insights allows this even in a more comfortable manner: it optimizes your images for you, minifries your javascripts and Css and serves them in a compressed file ready to download: isn't it great?
<p style="padding-left: 30px;"><span style="font-size: 12pt;"><em>About minified javascript and css used in prioritizing content process, you can learn more reading <a href="http://codingfix.com/improving-page-speed-prioritizing-content/">this article</a>.</em></span></p>

<h3>Option 2</h3>
You have even other options to compress and optimize your images, just to do your tests using already optimized content. The first option you have is to use online tools. For instance, <a href="https://compressor.io/">compressor.io</a> it's a fantastic tool which allow you to compress your images lossless and save hundreds of Kb, as they claim in their home page. If you use Wordpress, you can live without <a href="https://wordpress.org/plugins/wp-smushit/">SmushIt plugin</a> which optimizes your images while you are uploading them!
<h3>Option 3</h3>
You can also use a standalone software. The best one I have tried is certainly <a href="http://luci.criosweb.ro/riot/">Riot</a> (acronym which stands for Radical Image Optimization Tool): it is for Windows only, but you can easily run it from your Linux box using Wine. It does the magic! It can compress a 4500 x 3000 image of 19.4 Mb to a 5.09 Mb without resampling the image and without any visible loss of quality. If you resize the image to a more reasonable size in terms of width and height, let's say to a 1000 of width and 668 of height the new file weight will be 173.9 Kb!

I hope you have found useful this article. If you have some comment or some question, feel free to post a comment or to contact me through the <a href="http://codingfix.com/contacts/">Contact page</a>. And if you really feel generous, don't forget to share this post on your social networks! Thank you!

&nbsp;

The post [Improving page speed: optimizing images](https://codingfix.com/improving-page-speed-optimizing-images/) appeared first on [codingfix](https://codingfix.com).