The Magick
This will stack two images to create a CSS sprite. Very handy for flicker-free image rollovers:
$ montage -background transparent -tile 1x2 -geometry +0+2 image1.gif image2.gif result.gif
You could create a taller or wider stack by adjusting the -tile option. For example -tile 2x10 would stack 2 images across and 10 images down. If you don’t have 20 images, Imagemagick will leave empty spaces.
Not sure why I had to set -geometry +0+2. That creates a 2 pixel vertical offset, which was necessary to align to two images. If you leave it at -geometry +0+0 the hover image is slightly off, which makes for a cool “pressed in” effect, but it’s not always what I need.
The CSS
The CSS you would use to make the rollover work would go something like this:
a.rollover {
background: url('Images/result.gif') no-repeat center top;
display: block; /* necessary to set the width and height of a tags */
text-indent: -99999px; /* get the link text out of the way if replaced by an image */
overflow: hidden; /* so you don't get the dotted outline when you click on the link */
width: 100px;
height: 100px;
}
a.rollover:hover {
background: url('Images/result.gif') no-repeat center bottom;
}
Bash bonus!
When I did this, I had a folder of images with the naming scheme menu_img1.gif, menu_img1_on.gif, menu_img2.gif, menu_img2_on.gif.. .etc.. I automated the sprite creation process from the command line like this:
$ array=( img1 img2 img3 )
$ for i in 0 1 2
$ do
$ montage -background transparent -tile 1x2 -geometry +0+2 menu_${array[i]}.gif menu_${array[i]}_on.gif menu_${array[i]}_sprite.gif
$ done
One Comment
Use ‘seq’ with backticks and save a line and some thinking:
for i in `seq 1 3`;
do
montage -background transparent -tile 1×2 -geometry +0+2 menu_img${i}.gif menu_img${i}_on.gif menu_img${i}_sprite.gif
done
OS X doesn’t come with seq by default, but linux does and there are seq builds available for OS X.