Blog

Liquid Image

Look at the main page of this web site, you will find a photo of me. This image seems pretty normal at first glance, but it has one special feature: if you resize your browser window you can see the photograph’s dimensions adapts to the new column width. Let me explain how this has been done.

We call “liquid” a web site design that adapts itself to the browser window’s size. The image is liquid because its size changes too when the containing column size changes. This is how we do it.

Stylesheet

At first it looks rather simple. All that we have to do is to put an image on the page, while giving it a specific id attribute.

<img src="photo.jpg" id="photo" />

The id allows us to specify a stylesheet rule telling the image use 100% of the current block width:

#photo {
    width: 100%;
    height: auto;
}

As for the image height, it is set to auto. An automatic height will keep original image’s proportions.

(Note: the height attribute is by default set to auto. It is normally not necessary to write it explicitly. I did so here just to make things clearers.)

Image Quality

Now we need to be sure the picture has a sufficient resolution so that it looks good in any reasonable cases. We can guess that the image simply needs to be as big in pixel size as the maximum size we expect it to occupy on the page. Let’s check this.

This image makes 250 pixels width. It has been scaled down to a width of 200 pixels using Firefox on Mac OS X and on Windows XP. Observe the result.

Base Image
Shrunk on Mac OS X Shrunk on Windows XP

On the Mac as on Windows, the result shows nocks on the edges at regular interval on the image. Look at the zoomed-in version of the road to convince yourself. The phenomenon is easier to see on the image shrunk on Windows.

To downsize the image, the computer needs to remove some horizontal and vertical pixel lines, which produce nicks on oblique lines. If the result is better on the Mac, it’s simply because pixel lines are merged instead of plainly removed.

As a solution to this problem, we will use a bigger base image. The number of pixel lost will be greater when the browser resizes the image, but remaining pixel lines will be better distributed on the image. So we should obtain a better result. Let’s see…

Base Image
Shrunk on Mac OS X Shrunk on Windows XP

First thing to note is that the shrunk image no longer has the nocks it has before. It seems that we reached our goal. Except that there is something else that we should note by looking at differences between the two.

The picture shrunk on Windows is rougher. You may or may not like the effect, however if the picture had small-size details on it — like eyes on a face or the small branches of a tree — the result would not have been good at all. This is what we call aliasing. On the Mac, the picture is scaled down by merging lines, which removes this problem and makes the edges “smoother”.

Another problem is the one of the file size. In fact, base image’s dimensions in pixel have doubled from the first one, which means that we now have four times the pixels we had in the first image. Here, we the file size goes from 100 KB to 140 KB. The difference could be bigger or smaller depending of what you have in the picture: if it can easily be compressed to JPEG then the file size won’t matter much. But in every case, a small file is better than a bigger one.

The Final Solution

How to solve these two problems in one shot? We will make the image blurry. This way we solve the aliasing problem on Windows: it’s just like if we had merged pixels in advance just like the Mac would have done it.

And a blurry image can be compressed more with less quality lost. We can thus use a stronger JPEG compression for our image without really losing quality. Anyway, this won’t matter much since the picture is meant to be shrunk.

Base Image
Shrunk on Mac OS X Shrunk on Windows XP

Here, it’s the base image that looks the less appealing. However the reduced image on Mac and Windows are of acceptable quality. The result is an image that can adapt to any width, as long as we stay inside a reasonable range.

Mission accomplished.

Still a Problem with IE

As always, Internet Explorer, Windows’ version, has its own problems. Sporadically, the image height is wrong when the page loads. When in this state, a simple action like resizing the browser window gives it back a correct height.

To work around this, we can use a small JavaScript with, as the only goal, force a reflow of the page’s content around it. Here is such a script, a script that in normal circumstances wouldn’t do anything:

<script type="text/javascript">

function reflow() {
    var photo = document.getElementById("photo");
    photo.style.border = photo.style.border;
}

window.onload = reflow;

</script>

If you reload repeatedly the home page of my web site with Internet Explorer, you should be able to see the phenomenon of higher-than-should-be photograph that takes back the right height as soon as the script is run.

Conclusion

So that’s it. We saw that it’s not too much complicated to make a liquid image. The CSS rule width: 100% is almost all it takes. But work around an Internet Explorer you will need to add a small script that will force it to reflow the page once it’s loaded, otherwise we gets sporadically a distorted image with too much height.

The second thing to remember is that when the browser shrunk the image itself, we should provide a bigger base image. To keep the reduced image nice everywhere, it is a good idea to make the base image blurry a little. A blurry image is good for JPEG compression, so you may use that to reduce the file size.

That’s all. I hope that you liked my little tricks about liquid images.


Iraq still at war

Here in Canada, some US soldiers have deserted and are now asking for the refugee status. While they could be deported, some of them speak about what is happening in Iraq, and how the US army is fighting there.

This is an excerpt of “I can’t go back to Iraq”, a news story on The Dominion, where ex-solider Darrell Anderson talks about the procedure he had to follow:

At one point, he and a group of soldiers were stationed in front of a roadblock near an Iraqi police station. For several hours they sustained enemy fire. Several soldiers had died. Then, for a while, it was calm. Suddenly a car drove toward Anderson’s position. It had broken what soldiers call a “safety perimeter.” Also the car was emitting sparks, probably from bad brakes. Protocol in that situation is to shoot first and ask questions later, which is what Anderson’s fellow soldiers were yelling for him to do.

“It’s ok, it’s ok, it’s a family,” he yelled back.

Anderson held his fire. He had assumed the driver was confused, that he was trying to flee the city. He guessed right. Before the car sped away Anderson could make out two children sitting in the back seat. A boy and a girl, he thinks.

“Why didn’t you shoot?” some of the other soldiers asked him. “Next time you shoot,” they ordered.

“They got their procedures,” says Anderson. “Even if it is a family, you’re supposed to open fire, cause they broke the safety perimeter.”

Sad procedure.

It seems that beside the press releases, the occupying army is doing very little to convince Iraqis of their goodwill. I can understand why US soldiers are becoming paranoid with all these suicide bombings. But at the same time people living there who makes some missteps get killed by Americans, so it’s not so surprising many of them revolt and takes arms. How the situation is supposed get any better?

Anyway, his story gives more credibility to the continuing flow of reported atrocities commited in Iraq by coalition forces, like this one about people trying to escape fighting in Fallujah during the fight in November. From Dahr Jamail’s Iraq Dispatches weblog:

“The Americans shot every boat on the river because people were trying to escape Fallujah by the river. They shot all the sheep, any animal people owned was shot. Helicopters shot all the animals and anything that moved in all the villages surrounding Fallujah during the fighting.“

He said that none of the roads into Fallujah, or around Fallujah were passable because anyone on them was shot. “I know one family that were all killed. There are no signs on these roads that tell people not to use them — so people don’t know they aren’t supposed to use them. No signs in English or Arabic!”

What is missing is a reliable count of civilian casualties. Even something approximative would help us know the importance of this. But the United States refuse to release such numbers, while according to this other story from The Independant about humanitarian worker Marla Ruzicka (now dead), US troops does compile such statistics.

According to the same story, the number of civilian victims in this war is estimated to 100,000.

Can anybody stop this war?


PHP Markdown 1.0.1a

This is a small announcement for PHP Markdown 1.0.1a. This update corrects small problems regarding integration with WordPress. You can now insert blockquotes and automatic links in comments and the Markdown syntax will now be applied to syndication feeds.

By looking at the Subversion repository, it seems WordPress 1.5.1 will solve the last remaining issue. I’m talking about the issue of automatic links and HTML code samples dissapearing from auto-generated post and comment excerpts.


Will VLC survive?

Everyone knows about VideoLAN and their free software VLC which can play about any kind of video format you throw at it. It cold soon disappear, thanks to the new European software patents directive.

In fact, not just VLC is threatened. Many free software libraries and other projects VLC depends on could be forced to retirement. And this will affect other free players too.

The problem is that big corporations have patented about every methods that you can use to encode and decode video data. Until now, these patents where recognized only in the United States. So they where irrelevant to programmers elsewhere, like in Europe.

Two interesting links:

I’m glad there is no such thing in Canada, up to now at least…



  • © 2003–2025 Michel Fortin.