Jonathan Miles, Freelance Web Designer

Basic image optimisation mistakes (crimes against images)

January 10th, 2010

Image optimisation is about making images that are suitable for the web. That means reaching a balance between file size and quality, to ensure an image looks good and downloads fast.

There are a few mistakes that beginners often make.

Mistake 1: Unnecessarily big images

A classic mistake is to use a big, high resolution image without shrinking it properly. A big image has lots of pixels and each one adds to its file size, making it download slower.

The incorrect way to shrink an image is to write HTML code like this:

1
<img src="my-really-big-image.jpg" width="200" height="150" />

The code above tells the web browser to display “my-really-big-image.jpg” at a size of 200 x 150 pixels. If the image is 3000 x 2000 pixels then all that’s doing is displaying it at a smaller size. The image itself is still 3000 x 2000 pixels and that means it’s still going to download slowly.

Below is a simulation of what happens when a big image is displayed at a small size on a web page:

An unresized image downloads slowly

The image downloads slowly because there’s lots of pixels. It takes so long that you can see it loading. It also looks awful because there are jagged edges. Web browsers are bad at resizing images.

The correct way to shrink a big image is to use a graphics program to resize it. You end up with something like this:

A properly resized image

Notice how the quality is much better. The street light on the right is far less jagged and you can make out more detail. It also loads way faster. The original photo had a file size of 1.7mb but the small version is just 9kb. Or to it put another way, it’s 188 times smaller.

Real examples of images that are too big

Using bigger images than needed is a common mistake. It’s easy to find examples with the help of Google:

Google image search: “the team”

The above google search looks for large images on pages containing the phrase “the team”. This tends to find pages where organisations are introducing their staff members and haven’t resized the team photo properly.

Mistake 2: Wrong image format

Image data can be stored in different formats. There are lots of formats and each has its own strengths and weaknesses, so choosing the right one is important.

Most people are probably familiar with the JPG format because it’s great for photos. That doesn’t mean it’s good for everything though. JPG is bad for images that have lots of solid colour and crisp lines. It’s much better to use the PNG format for those kind of images. Take a look at this zoomed in comparison:

The PNG on the left is perfect. The JPG on the right has compression artifacts.

The PNG on the left is perfect. The JPG on the right has compression artifacts.

JPG is a lossy image format. That means it compresses the image data by discarding parts the human eye won’t notice. It works well for photos but the clean, crisp lines of an illustration can suffer from noticeable compression artifacts.

PNG on the other hand is lossless, so it can preserve images perfectly. It works best for images with clean, crisp lines such as vector art work, logos, certain comics and so on.

This comic showing PNG vs JPEG makes the point well (with a tiny bit of exaggeration).

Mistake 3: Squashing people (aspect ratio abuse)

I recently came across a website where the client had created a banner graphic from a photo but completely ignored the aspect ratio, so it looked like this:

It looks squashed because the aspect ratio is wrong

It looks squashed because the aspect ratio is wrong

The image above is 400 pixels wide and 50 pixels tall but you can see that it’s been squashed vertically, making the people look short and fat. That’s what happens when you get the aspect ratio wrong.

The aspect ratio is found by dividing the image’s width by its height, so in this case we do 400 / 50 = 8. That gives an aspect ratio of 8:1, meaning that for every 8 pixels of width there’s 1 pixel of height. To create an image that isn’t squashed, the original photo will have to be cropped to the correct aspect ratio.

Photoshop has a Rectangular Marquee tool that can be set to a fixed aspect ratio:

Using Photoshop's fixed ratio selection

Once you’ve set the desired ratio you can draw a selection and it’ll always be the right aspect ratio. It’s then just a case of cropping to your selection and saving.

The people aren't squashed now the aspect ratio's correct

The people aren't squashed now the aspect ratio's correct

That looks better. Notice how you don’t see as much of their bodies as before – that’s because those parts had to be cropped out. The first image looked squashed because it was cramming an image that was too tall into the same space.

The end

If you’re not making any of the basic mistakes that have been discussed here then you’re probably doing okay.

New website design

September 11th, 2009

Crayons

My colourful new website design makes a change from the last one – you can see my previous designs over on the design page.

My starting point for this latest design was a blog post showing how to make abstract art with Illustrator. By using the twirl tool it’s possible to create some nice swirly effects, though I made sure not to get too carried away.

The new website design, viewed using a widescreen resolution

The new website design, viewed using a widescreen resolution

I wanted this design to be attention grabbing, so I wrote a jQuery script to make an animated TV screen effect. I used the Preload plugin, written by Ariel Flesler, to preload the images used by the TV. This means there’s no unsightly delay when changing between images, because the next one is already in the browser’s cache. Each image is loaded just before it’s needed, so the page isn’t bogged down with lots of concurrent image requests, which keeps everything fast.

Resizing images with jQuery

September 7th, 2009

jQuery

The other week I came across a post about resizing images with jQuery. The idea is simple: use JavaScript to detect the size of the browser window (or whatever metric works best for you) and then replace any images with smaller versions if needed.

My immediate thought was that this technique would work great on the splash page of the Insect Dynasty website that I worked on recently, since the splash image was slightly larger than I would’ve liked when viewed at 1024 x 768:

What a visitor would have seen at 1024 x 768

Before: what a visitor would have seen at 1024 x 768

Even though you really need to be using a higher resolution to view the artwork on the website easily I wasn’t happy to leave the splash page like this. Besides, I wanted to try this technique out. So I wrote an inline script for the splash page using jQuery:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
jQuery(document).ready(function() {

        // Switches between the small and large splash image as appropriate.
        function resizeSplash() {
       
            var $window = jQuery(window);
            var $body = jQuery("body");
            var $enter = jQuery("#enter");
           
            if ($window.height() < 750) {
                $body.css({
                    backgroundPosition: "center 0",
                    backgroundImage: "url('<?php bloginfo('template_url'); ?>/images/splash-background-small.jpg')",
                    minHeight: "0"
                });
                $enter.css("marginTop", "375px");
            } else {
                $body.removeAttr("style");
                $enter.removeAttr("style");
            }
        }

        // Initial call to image resizing.
        resizeSplash();

        // Resizes the splash image, if needed, when the visitor resizes their browser.
        jQuery(window).bind("resize", function() {
            resizeSplash();  
        }
       
    );

How it works

The key part of the script is where it checks if the browser viewport is less than 750 pixels tall – if so the background image on the body element is swapped. I also make some adjustments to the background-position and min-height properties, as these are also set in the stylesheet and need to be overridden as well when changing to a smaller splash image.

When the viewport height is 750 pixels or greater I simply remove the inline style attributes that jQuery would have set via calls to the css() function. This is the best way to revert back to using the larger splash image because it’s simple code, which is always a good thing – it makes the browser go back to using the rules from the stylesheet. That’s much better than making another call to css(), which would require duplicating bits of style definitions unnecessarily.

The technique relies on JavaScript being enabled so there may be some visitors who don’t enjoy its benefit. But it’s still an improvement for the people who do use JavaScript, as you can see:

With a smaller image the splash page fits nicely

After: with a smaller image the splash page fits nicely

The evil of splash pages

As an aside I’m aware that splash pages aren’t all that popular – they’re often just an unwanted obstacle for visitors, so there aren’t that many cases where I’d recommend using one on a website (not without really thinking it over carefully). In this case the splash page is partly setting the tone for the website and, being a website with a strong artistic focus, I consider the splash image to be part of the website’s content.

As splash pages go it’s also rather lightweight, unlike the frankly obnoxious ones you sometimes see which have heavyweight flash animations. That’s very difficult to pull off.

Cufon ignoring font colours

September 1st, 2009

Cufon Logo

Recently I was working on a website and using the rather natty Cufon (a clever bit of JavaScript) to do some font replacement. Or at least that was my intention – for some reason it was ignoring the font colour I’d set in my stylesheet…

Always add stylesheets before scripts

The problem was that I’d inadvertently linked my stylesheet into the page after the Cufon script, whereas styling should come before scripts. This is covered in Cufon’s styling documentation – by linking stylesheets into the page first you’re guaranteeing they’ll load before Cufon tries to replace any text. Do it the wrong way around and your style rules won’t necessarily be available by the time Cufon tries to work its magic.

This also explains why the problem was intermittent – in some cases the stylesheet must have been cached, which side-stepped the timing problems.

It’s good practice in general to link external stylesheets before external JavaScript files, as it can help your web pages to load faster. This is because web browsers won’t download subsequent resources once they find a script to download – scripts are capable of modifying the page, so the browser needs to download them fully before continuing. By putting other resources before the scripts they’ll be started first and will download in parallel (there’s a limit to how many files can download in parallel, but it’s nevertheless worth taking advantage of parallelism).

Night Design: Retrospective

April 13th, 2009

Night Design

The default design for my website is called “Night”. It’s a particularly graphical design that is intended to be both visually striking and distinctive. I was inspired to take this approach after reading an article about websites with large backgrounds. One of my aims in creating different designs for my website is to make them as diverse as possible – to try and push my skills and, to some extent, CSS itself. It was therefore an easy decision to make a design that used a large background.

Continue reading…

Design Selector

February 8th, 2009

Pencil on a desk

I regularly step back from my website and try to identify things I can improve. It might be small things like adjusting link colours to increase contrast, or increasing line heights to aid readability. My latest decision has been to alter this website’s navigation. Specifically, I have promoted my website design selector to its own page.

Previously the design selector was tucked away at the very bottom of the home page – a place where, unsurprisingly, people rarely look. That was far from ideal for such a useful feature, so it makes much more sense to give the design selector its own page and a prominent link in the site navigation.

So there you have it – if you don’t like the way my website looks, go to the design page and change it.

Night Design

January 11th, 2009

The "Night" designIt’s a new year so I decided it was about time to put up my new website design. There are still one or two touches I’d like to add to it but all the essentials are in place. This new design uses a large background, which is a great way to create visual impact. Once I’ve finished polishing the design I’ll write an article on how I went about creating it.

IntenseDebate

November 24th, 2008

IntenseDebateMost blogs and websites these days have commenting systems, allowing visitors to feedback and get involved. Quite often I find that visitor comments are just as interesting as the article that prompted them, if not more so. Therefore I decided it’s about time I put a commenting system in place on my own website.

Continue reading…

A web 2.0 redesign

July 1st, 2007

cake-designBehold the new design for my website. I was motivated to redesign for a couple of reasons. The first is what I’m going to call “designer’s itch” – the uncontrollable urge to make something new. Secondly (and most importantly), I stumbled upon some websites that looked nicer than mine. Energised with new feelings of self-doubt and insecurity, I promptly opened Photoshop with the aim of giving this website more of a “wow” factor.

Have I trotted out several web 2.0 cliches in my redesign? Yes. Is the pink colour scheme too effeminate for a man like myself? Most probably. Despite this I really like the new look. Well, for now at least…