-
Notifications
You must be signed in to change notification settings - Fork 25
/
convert-ico-to-png
executable file
·164 lines (126 loc) · 3.95 KB
/
convert-ico-to-png
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/sh
set -euf
## Program Tracking ##
program_command="convert-ico-to-png"
program_version="3.0.0"
program_created="2015-05-30"
program_updated="2016-02-14"
program_license="GPL"
program_contact="Joel Parker Henderson ([email protected])"
program_variant="$program_command version $program_version updated $program_updated"
## Help ##
help(){
cat <<END
convert-ico-to-png:
convert an ICO image file to a PNG image file.
Syntax:
convert-ico-to-png
[--input <input ico file name>]
[--output <output png file name]
[--size <pixel width>]
[--srcset]
Example with defaults:
convert-ico-to-png
Example with specifics:
convert-ico-to-png --input favicon.ico --output favicon.png --size 16
Options:
* \`-i\`, \`--input\`: input file name; default is "favicon.ico"
* \`-o\`, \`--output\`: output file name; default is "favicon.png"
* \`-s\`, \`--size\`: size as a pixel width; default is 16.
* \`--srcset\`: generate a source set of icons using sizes 16, 32, 48, 64, and symlines.
## Source set details ##
The \`--srcset\` option generates our preferred image source set,
The goal is to make the files work with this kind of HTML that provides four image scales:
<img src="foo.png" srcset="foo-1x.png 1x, foo-2x.png 2x, foo-3x.png 3x, foo-4x.png 4x">
We prefer this file naming convention for source set image files:
* append the base pixel size
* such as "16x16", "32x32", "48x48", "64x64"
We prefer this file naming convention for source set image symlinks:
* append the scale multiplier
* such as "1x", "2x", "3x", "4x".
This tool generates these files:
* \`foo-16x16.png\`
* \`foo-32x32.png\`
* \`foo-48x48.png\`
* \`foo-64x64.png\`
This tool generates these symlinks:
* \`foo-16x16-1x.png\` => \`foo-16x16-1x.png\`
* \`foo-16x16-2x.png\` => \`foo-16x16-1x.png\`
* \`foo-16x16-3x.png\` => \`foo-16x16-1x.png\`
* \`foo-16x16-4x.png\` => \`foo-16x16-1x.png\`
If you prefer other conventions, then please let us know, and we'll do
our best to add your conventions as options in this tool.
Pull requests are welcome.
## Requirements ##
This script needs:
* The \`convert\` command, typically provided by ImageMagick, or GraphicsMagick, or any command-line compatible graphics tool.
Command: $program_command
Version: $program_version
Created: $program_created
Updated: $program_updated
License: $program_license
Contact: $program_contact
END
}
## Essential Functions ##
out () { printf %s\\n "$*" ; }
err () { >&2 printf %s\\n "$*" ; }
die () { >&2 printf %s\\n "$*" ; exit 1 ; }
die_opt_unk() { die "Option $1 is unknown" ; }
die_opt_arg() { die "Option $1 needs an argument" ; }
## Defaults ##
input_file_name="favicon.ico"
output_file_name="favicon.png"
pixel_width=16
## Options ##
while [ $# -gt 0 ]; do
case $1 in
-- )
shift
break
;;
-h | --help )
help
;;
-V | --version )
out $program_variant
;;
-i | --input )
[ $# -le 1 ] && die_opt_arg $1
input_file_name=$2
shift
;;
-o | --output )
[ $# -le 1 ] && die_opt_arg $1
output_file_name=$2
shift
;;
-s | --size )
[ $# -le 1 ] && die_opt_arg $1
pixel_width=$2
shift
;;
--srcset )
srcset=true
;;
-?*)
die_opt_unk $1
;;
*)
break
esac
shift
done
## Main ##
if [ "$srcset" = true ]; then
output_file_base="${output_file_name%.*}"
for n in 1 2 3 4; do
size=$(($n * $pixel_width))
output_file_name_now="${output_file_base}-${size}x${size}.png"
output_symlink_now="${output_file_base}-${pixel_width}x${pixel_width}-${n}x.png"
convert "$input_file_name" -thumbnail "${size}x${size}" -alpha on -background none -flatten "$output_file_name_now"
ln -sfn "$output_file_name_now" "$output_symlink_now"
done
else
convert "$input_file_name" -thumbnail "${pixel_width}x${pixel_width}" -alpha on -background none -flatten "$output_file_name"
fi