How to create an image sprite? (max width 2042px)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
buraitopengin
Posts: 1
Joined: 2011-03-27T04:40:17-07:00
Authentication code: 8675308

How to create an image sprite? (max width 2042px)

Post by buraitopengin »

Hi,

I just started using ImageMagick from the command line and I must say, this tool is pretty amazing!
GD library don't give me the flexibility i need for my projects and ImageMagick have a lot to offer.

Moving on, I got a few questions about sprite generation and "conditional rules" that must be applied in order to generate cross browsers compatible CSS sprite based on .png images.

Here's what i'm trying to do :
1/ I got 60+ images in a folder with homogeneous widths and heights (24px X 24px).
2/ I have to increase the canvas size of each images in order to prevent images to be glued to one another (2px of padding).
3/ Due to an Opera bug, the final sprite should never exceed 2042 pixels in width (does "+append" have some sort of callback to check the final image width?)

Do you think that can be done in one command line or should i make a shell script to loop on each images?

Thanks a lot :wink:

EDIT

I made some basic test and :

Code: Select all

montage myimages/*.png -tile 78x -geometry 24x24+1+1 sprite.png
Seems to do the job quite well, but still, i have to specify the geometry and the tile.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to create an image sprite? (max width 2042px)

Post by fmw42 »

(does "+append" have some sort of callback to check the final image width?)
Not in command line (which is all I know).

You should be able to do it with +append (for a single row of images), except for the width check. (But you can do the width test with an if test and a calculation based upon your image size and border and number of images).

For example:

n=4
size=`convert rose: -ping -format "%[fx:4*(w+2)]" info:`
if [ $size -le 2042 ]; then
convert rose: rose: rose: rose: -bordercolor white -border 1 +append rose4_append.png
else
echo "sprite is too wide"
fi

If you don't want the white border at the top, then you can use -extent. see http://www.imagemagick.org/Usage/crop/#extent
or -splice (twice -- one for left and one for right). see http://www.imagemagick.org/Usage/crop/#splice


P.S. This also works with montage in a way that you don't need the size or number of images (to generate one row). You still need to do some kind of test first to be sure you don't exceed the desired max width.

montage rose: rose: rose: rose: -background white -geometry +1+1 -tile x1 rose4_montage.png

P.S. 2 You can also precompute the number of images to put in one row for montage. For example:

width=70
maxwidth=216
num=`convert xc: -format "%[fx:floor($maxwidth/($width+2))]" info:`
montage rose: rose: rose: rose: rose: rose: -background white -tile ${num}x -geometry +1+1 rose6_3x2_montage.png
Last edited by fmw42 on 2011-03-28T10:26:47-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to create an image sprite? (max width 2042px)

Post by anthony »

buraitopengin wrote:does "+append" have some sort of callback to check the final image width?
No it does not need one. All the images it is appending is in memory, so it just adds up all the image widths, create a new canvas (of the current background color) and then 'Copy Compose' the images onto it.

That means that images will transparency remain transparent, but any parts of the appended image not covered by a image will have the background color.

Try this and you will see what I mean...

convert rose: http://www.imagemagick.org/Usage/images/hand_point.gif rose: \
-background white +append show:

The area around the 'hand' is transparent, but below it the image is white.


Note that for montage the images are all fit into a fixed and constant sized 'cell'. Each cell has a fixed frame, label, border added around it (according to the various options, but especially the geometry offset) before it is overlaid on the generated 'page' canvas, according to the -tile setting.


I do have on my (long) To Do list a set of image layout functions, beyond what is provided by append, and montage (array layout), that would include things like 'layout in rows up to WIDTH, then go to next row, up to optional HEIGHT (page height). When I ever get time to implement is a completely different matter.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply