Optimize PNG images with zopfli
[phpmyadmin.git] / scripts / generate-sprites
blob3f4c9c20139cf226af4b572f691daef47b67c174
1 #!/bin/sh
2 # vim: expandtab sw=4 ts=4 sts=4:
4 # Do not run as CGI
5 if [ -n "$GATEWAY_INTERFACE" ] ; then
6 echo 'Can not invoke as CGI!'
7 exit 1
8 fi
10 # Check for proper number of command line args.
11 if [ $# -ne 1 ]; then
12 echo "Usage: `basename $0` {path_to_pma_root_folder}"
13 exit 65
16 # Check if we have ImageMagick
17 hash identify 2>&- || {
18 echo "ERROR: ImageMagick not found on the system!"
19 echo "Quitting..."
20 exit 1
23 # Compress image, if possible
24 HAVE_PNGCRUSH=1
25 hash pngcrush 2>&- || {
26 HAVE_PNGCRUSH=0
27 echo "WARNING: 'pngcrush' not found, will not be able to compress the sprites"
30 # Icons that should not be included in the sprite
31 BLACKLIST="vertical_line.png spacer.png"
33 # Output filename for the sprite image
34 OUTPUT="sprites.png"
36 # Library file that will contain the information about
37 # individual images that are part of the sprite
38 LIBRARY="../sprites.lib.php"
40 if [ -d $1/themes ]; then
41 cd $1/themes
43 # For each theme
44 for d in $(ls -d */); do
45 # Go to folder that contains the images
46 cd "$d"img
47 echo "Processing folder: $PWD"
48 FILES=''
49 for f in $(ls *.png| LC_ALL=C sort); do
50 VALID=true
51 # Do not include blacklisted icons
52 for b in $BLACKLIST; do
53 if [ "$b" = "$f" ]; then
54 VALID=false
56 done
57 if [ $VALID = false ]; then
58 continue
60 DATA=$(identify -ping $f || echo "NULL")
61 if [ "$DATA" != "NULL" ]; then
62 SIZE=$(echo $DATA | cut -d ' ' -f 3 | sed 's/x/ /')
63 # Do not include icons that are larger than 16x16
64 for s in $SIZE; do
65 if [ $s -gt 16 ]; then
66 VALID=false
68 done
69 if [ $VALID = true ]; then
70 # Build the list of valid icons
71 FILES="$FILES $f"
74 done
76 # Create an empty sprite of the correct size
77 NUM_FILES=''
78 for f in $FILES; do
79 NUM_FILES=$(($NUM_FILES+1))
80 done
81 convert -size 16x$(($NUM_FILES*16+16)) xc:none temp.png
83 # Add each icon to the sprite
84 CURRENT=1
85 for f in $FILES; do
86 convert temp.png $f -geometry +0+$(($CURRENT*16)) -composite temp.png
87 CURRENT=$(($CURRENT+1))
88 done
90 # Compress image, if possible
91 if [ $HAVE_PNGCRUSH -eq 1 ]; then
92 echo "Compressing file: $PWD/$OUTPUT"
93 pngcrush -brute temp.png $OUTPUT > /dev/null
94 rm -f temp.png
95 else
96 mv temp.png $OUTPUT
99 # Generate the library file that contains the information
100 # about individual images that are part of the sprite
101 echo '<?php' > $LIBRARY
102 echo '/**' >> $LIBRARY
103 echo ' * AUTOGENERATED CONTENT - DO NOT EDIT!' >> $LIBRARY
104 echo ' * ALL CHANGES WILL BE UNDONE!' >> $LIBRARY
105 echo ' * RUN `./scripts/generate-sprites` TO UPDATE THIS FILE' >> $LIBRARY
106 echo ' *' >> $LIBRARY
107 echo ' * @package PhpMyAdmin-theme' >> $LIBRARY
108 echo ' */' >> $LIBRARY
109 echo '' >> $LIBRARY
110 echo '/**' >> $LIBRARY
111 echo ' * Map of sprites inside sprite.png' >> $LIBRARY
112 echo ' */' >> $LIBRARY
113 echo "\$sprites = array(" >> $LIBRARY
114 CURRENT=1
115 for f in $FILES; do
116 # Add a CSS rule for each icon in the sprite
117 NAME=$(echo "'$f'" | sed 's/\.png//')
119 DATA=$(identify -ping $f || echo "NULL")
120 if [ "$DATA" != "NULL" ]; then
121 SIZE=$(echo $DATA | cut -d ' ' -f 3 | sed 's/x/ /')
122 WIDTH=0
123 HEIGHT=0
124 for s in $SIZE; do
125 if [ $WIDTH = 0 ]; then
126 WIDTH=$s
127 else
128 HEIGHT=$s
130 done
132 echo " $NAME => array(" >> $LIBRARY
133 echo " 'position' => '$CURRENT'," >> $LIBRARY
134 echo " 'width' => '$WIDTH'," >> $LIBRARY
135 echo " 'height' => '$HEIGHT'" >> $LIBRARY
136 echo " )," >> $LIBRARY
137 CURRENT=$(($CURRENT+1))
138 done
139 echo ");" >> $LIBRARY
141 # Back to the parent folder
142 cd ../..
143 done
144 exit 0
145 else
146 echo "ERROR: could not find the 'themes' folder in '`readlink -f $1`'"
147 exit 1