Blame


1 ddc03123 2020-03-28 op CSS sprites are a nice idea, stolen from the game developers I think,
2 ddc03123 2020-03-28 op that consist to merge all the images you need in a single one and
3 ddc03123 2020-03-28 op download only that. I will not prove that it's best than downloading
4 ddc03123 2020-03-28 op *n* images, but it should clear that downloading a file, albeit a bit
5 ddc03123 2020-03-28 op bigger, requires less TCP segments going back and forth.
6 ddc03123 2020-03-28 op
7 ddc03123 2020-03-28 op The other day I needed a CSS sprite. Being the lazy person I am, right
8 ddc03123 2020-03-28 op after The GIMP was open I thought that I didn't want to do the math.
9 ddc03123 2020-03-28 op
10 ddc03123 2020-03-28 op After a bit of searching, I found a post where the author wrote a
11 ddc03123 2020-03-28 op script to generate the sprites (and the relative css file) with image
12 ddc03123 2020-03-28 op magick. I have lost that link, and the history of my browser doesn't
13 ddc03123 2020-03-28 op help. I remember that the domain name contains "php", but nothing
14 ddc03123 2020-03-28 op more. However, I rewritten the script, so it better fits my needs.
15 ddc03123 2020-03-28 op
16 ddc03123 2020-03-28 op The idea is to join all the image using `convert(1)` and then generate
17 ddc03123 2020-03-28 op a CSS file with the offsets using `identify(1)`. Both programs should
18 ddc03123 2020-03-28 op come along with imagemagick.
19 ddc03123 2020-03-28 op
20 ddc03123 2020-03-28 op Here's the script
21 ddc03123 2020-03-28 op
22 ddc03123 2020-03-28 op #!/bin/sh
23 ddc03123 2020-03-28 op
24 ddc03123 2020-03-28 op convert sprites/* -append img/sprites.png
25 ddc03123 2020-03-28 op
26 ddc03123 2020-03-28 op for i in sprites/*; do
27 ddc03123 2020-03-28 op identify -format '%W %H %f\n' $i
28 ddc03123 2020-03-28 op done | awk '
29 ddc03123 2020-03-28 op function fname(path) {
30 ddc03123 2020-03-28 op sub (".png", "", path)
31 ddc03123 2020-03-28 op return path
32 ddc03123 2020-03-28 op }
33 ddc03123 2020-03-28 op
34 ddc03123 2020-03-28 op BEGIN {
35 ddc03123 2020-03-28 op y = 0
36 ddc03123 2020-03-28 op
37 ddc03123 2020-03-28 op print "%sprite-base {"
38 ddc03123 2020-03-28 op print " background-image: url(/img/sprites.png);"
39 ddc03123 2020-03-28 op print " background-repeat: no-repeat;"
40 ddc03123 2020-03-28 op print "}"
41 ddc03123 2020-03-28 op print ""
42 ddc03123 2020-03-28 op }
43 ddc03123 2020-03-28 op
44 ddc03123 2020-03-28 op {
45 ddc03123 2020-03-28 op width = $1
46 ddc03123 2020-03-28 op height = $2
47 ddc03123 2020-03-28 op class = fname($3)
48 ddc03123 2020-03-28 op
49 ddc03123 2020-03-28 op print "%sprite-" class " {"
50 ddc03123 2020-03-28 op print " @extend %sprite-base;"
51 ddc03123 2020-03-28 op if (y == 0)
52 ddc03123 2020-03-28 op print " background-position: 0 0;"
53 ddc03123 2020-03-28 op else
54 ddc03123 2020-03-28 op print " background-position: 0 -" y "px;"
55 ddc03123 2020-03-28 op print "}"
56 ddc03123 2020-03-28 op print ""
57 ddc03123 2020-03-28 op
58 ddc03123 2020-03-28 op y += height;
59 ddc03123 2020-03-28 op }
60 ddc03123 2020-03-28 op ' > scss/_sprites.scss
61 ddc03123 2020-03-28 op
62 ddc03123 2020-03-28 op Assuming that the images are within `sprites/` and all of them are png
63 ddc03123 2020-03-28 op files, this script will generate the sprite in `img/sprites.png` and a
64 ddc03123 2020-03-28 op SASS file in `sass/_sprits.scss` with one placeholder for every image
65 ddc03123 2020-03-28 op (the naming scheme is `%sprite-$NAMEFILE` with the `$NAMEFILE` being
66 ddc03123 2020-03-28 op the file name without extension).
67 ddc03123 2020-03-28 op
68 ddc03123 2020-03-28 op It should be trivial to edit to handle pure CSS output, and eventually
69 ddc03123 2020-03-28 op other image formats.