-
Notifications
You must be signed in to change notification settings - Fork 4
/
icon_generator.sh
executable file
·551 lines (510 loc) · 13.1 KB
/
icon_generator.sh
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#!/bin/bash
usage() {
echo
echo Usage: icon_generator.sh -i image file -p platform -t icon type [-d directory] [-c color] [-w] [-m]
echo
echo " -i The original image file."
echo " -p The icon's platform. Valid options:"
echo " ios"
echo " android"
echo " -t Most iOS and Anroid icons are supported. Valid options:"
echo " toolbar (iOS) (1x = 22x22)"
echo " tabbar (iOS) (1x = 25x25)"
echo " tableviewcell (iOS) (1x = 25x25)"
echo " app (iOS)"
echo " watch (iOS)"
echo " action (Android)"
echo " notification (Android)"
echo " small (Android)"
echo " custom (Android and iOS)"
echo " -s Optional. The size of the new icon; used for -t == custom"
echo " -s 100"
echo " -d The directory the images will be saved to. Default is the current directory."
echo " On iOS, icons, along with a Contents.json file, are saved in a .imageset file."
echo " On Android, a new directory for each density is created, if it doesn't already exist."
echo " -c Optional. A color used to mask the original image. Example: blue, \"#929292\"."
echo " Default keeps the original color."
echo " This flag does not apply to -type app."
echo " -w Optional. Removes whitespace around the original image."
echo " Default keeps the original whitespace."
echo " This flag does not apply to -type app."
echo " -m Optional. Enables Google Material Design icon sizes (slightly smaller than normal)."
echo " This flag only applies to Android."
echo
echo ./icon_generator.sh -i ic_trash.png -p ios -t toolbar -d ~/icons/ -c red
echo
echo This script requires ImageMagick for image manipulation.
echo https://www.imagemagick.org/script/index.php
echo
}
TRIM=0
COLORIZE=0
MATERIAL=0
while getopts "i:p:t:s:d:c:wm" OPTION
do
case $OPTION in
i)
IMG=$OPTARG
;;
p)
PLATFORM=$OPTARG
;;
t)
TYPE=$OPTARG
;;
s)
SIZE=$OPTARG
;;
d)
DIR=$OPTARG
;;
c)
COLORIZE=1
COLOR=$OPTARG
;;
w)
TRIM=1
;;
m)
MATERIAL=1
;;
esac
done
# Argument validation.
if [ -z $IMG ]; then
echo ERROR: Image file missing.
usage
exit 1
fi
if [ -z $PLATFORM ]; then
echo ERROR: Platform missing.
usage
exit 1
fi
if [ -z $TYPE ]; then
echo ERROR: Icon type not supplied.
usage
exit 1
fi
if [ $TYPE = custom ] && [ -z $SIZE ]; then
echo ERROR: Size not specified for type 'custom'.
usage
exit 1
fi
# File extension of the input image.
EXT=.${IMG##*.}
# File name of the input image, sans extension.
FILE_NAME=${IMG%.*}
ORIGINAL_FILE_NAME=$FILE_NAME
# Creates a given directory if it doesn't already exist.
makeDir() {
if [ ! -d "$1" ]; then
mkdir $1
fi
}
# iOS app icon
if [ $PLATFORM = ios -a $TYPE = app ]; then
# Create .imageset directory.
DIR=$DIR$ORIGINAL_FILE_NAME".appiconset"
makeDir $DIR
FILE_20x20=$FILE_NAME"_20"$EXT
FILE_29x29=$FILE_NAME"_29"$EXT
FILE_40x40=$FILE_NAME"_40"$EXT
FILE_40x40_1=$FILE_NAME"_40-1"$EXT
FILE_40x40_2=$FILE_NAME"_40-2"$EXT
FILE_60x60=$FILE_NAME"_60"$EXT
FILE_58x58=$FILE_NAME"_58"$EXT
FILE_58x58_1=$FILE_NAME"_58-1"$EXT
FILE_76x76=$FILE_NAME"_76"$EXT
FILE_87x87=$FILE_NAME"_87"$EXT
FILE_80x80=$FILE_NAME"_80"$EXT
FILE_80x80_1=$FILE_NAME"_80-1"$EXT
FILE_120x120=$FILE_NAME"_120"$EXT
FILE_120x120_1=$FILE_NAME"_120-1"$EXT
FILE_152x152=$FILE_NAME"_152"$EXT
FILE_167x167=$FILE_NAME"_167"$EXT
FILE_180x180=$FILE_NAME"_180"$EXT
FILE_1024x1024=$FILE_NAME"_1024"$EXT
convert $IMG -resize 20x20 $DIR/$FILE_20x20
convert $IMG -resize 29x29 $DIR/$FILE_29x29
convert $IMG -resize 40x40 $DIR/$FILE_40x40
convert $IMG -resize 40x40 $DIR/$FILE_40x40_1
convert $IMG -resize 40x40 $DIR/$FILE_40x40_2
convert $IMG -resize 60x60 $DIR/$FILE_60x60
convert $IMG -resize 58x58 $DIR/$FILE_58x58
convert $IMG -resize 58x58 $DIR/$FILE_58x58_1
convert $IMG -resize 76x76 $DIR/$FILE_76x76
convert $IMG -resize 87x87 $DIR/$FILE_87x87
convert $IMG -resize 80x80 $DIR/$FILE_80x80
convert $IMG -resize 80x80 $DIR/$FILE_80x80_1
convert $IMG -resize 120x120 $DIR/$FILE_120x120
convert $IMG -resize 120x120 $DIR/$FILE_120x120_1
convert $IMG -resize 152x152 $DIR/$FILE_152x152
convert $IMG -resize 167x167 $DIR/$FILE_167x167
convert $IMG -resize 180x180 $DIR/$FILE_180x180
convert $IMG -resize 1024x1024 $DIR/$FILE_1024x1024
# Create Contents.json file.
cat << EOF > $DIR/Contents.json
{
"images" : [
{
"size" : "20x20",
"idiom" : "iphone",
"filename" : "$FILE_40x40_1",
"scale" : "2x"
},
{
"size" : "20x20",
"idiom" : "iphone",
"filename" : "$FILE_60x60",
"scale" : "3x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "$FILE_58x58",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "$FILE_87x87",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "$FILE_80x80",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "$FILE_120x120",
"scale" : "3x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "$FILE_120x120_1",
"scale" : "2x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "$FILE_180x180",
"scale" : "3x"
},
{
"size" : "20x20",
"idiom" : "ipad",
"filename" : "$FILE_20x20",
"scale" : "1x"
},
{
"size" : "20x20",
"idiom" : "ipad",
"filename" : "$FILE_40x40",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "$FILE_29x29",
"scale" : "1x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "$FILE_58x58_1",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "$FILE_40x40_2",
"scale" : "1x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "$FILE_80x80_1",
"scale" : "2x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "$FILE_76x76",
"scale" : "1x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "$FILE_152x152",
"scale" : "2x"
},
{
"size" : "83.5x83.5",
"idiom" : "ipad",
"filename" : "$FILE_167x167",
"scale" : "2x"
},
{
"size" : "1024x1024",
"idiom" : "ios-marketing",
"filename" : "$FILE_1024x1024",
"scale" : "1x"
}
],
"info" : {
"version" : 1,
"author" : "ios_icon_set"
}
}
EOF
exit 1
fi
# iOS watch icon
if [ $PLATFORM = ios -a $TYPE = watch ]; then
# Create .imageset directory.
DIR=$DIR$ORIGINAL_FILE_NAME".appiconset"
makeDir $DIR
FILE_48x48=$FILE_NAME"_48"$EXT
FILE_55x55=$FILE_NAME"_55"$EXT
FILE_58x58=$FILE_NAME"_58"$EXT
FILE_87x87=$FILE_NAME"_87"$EXT
FILE_80x80=$FILE_NAME"_80"$EXT
FILE_88x88=$FILE_NAME"_88"$EXT
FILE_100x100=$FILE_NAME"_100"$EXT
FILE_172x172=$FILE_NAME"_172"$EXT
FILE_196x196=$FILE_NAME"_196"$EXT
FILE_216x216=$FILE_NAME"_216"$EXT
FILE_1024x1024=$FILE_NAME"_1024"$EXT
convert $IMG -resize 48x48 $DIR/$FILE_48x48
convert $IMG -resize 55x55 $DIR/$FILE_55x55
convert $IMG -resize 58x58 $DIR/$FILE_58x58
convert $IMG -resize 87x87 $DIR/$FILE_87x87
convert $IMG -resize 80x80 $DIR/$FILE_80x80
convert $IMG -resize 88x88 $DIR/$FILE_88x88
convert $IMG -resize 100x100 $DIR/$FILE_100x100
convert $IMG -resize 172x172 $DIR/$FILE_172x172
convert $IMG -resize 196x196 $DIR/$FILE_196x196
convert $IMG -resize 216x216 $DIR/$FILE_216x216
convert $IMG -resize 1024x1024 $DIR/$FILE_1024x1024
# Create Contents.json file.
cat << EOF > $DIR/Contents.json
{
"images" : [
{
"size" : "24x24",
"idiom" : "watch",
"filename" : "$FILE_48x48",
"scale" : "2x",
"role" : "notificationCenter",
"subtype" : "38mm"
},
{
"size" : "27.5x27.5",
"idiom" : "watch",
"filename" : "$FILE_55x55",
"scale" : "2x",
"role" : "notificationCenter",
"subtype" : "42mm"
},
{
"size" : "29x29",
"idiom" : "watch",
"filename" : "$FILE_58x58",
"role" : "companionSettings",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "watch",
"filename" : "$FILE_87x87",
"role" : "companionSettings",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "watch",
"filename" : "$FILE_80x80",
"scale" : "2x",
"role" : "appLauncher",
"subtype" : "38mm"
},
{
"size" : "44x44",
"idiom" : "watch",
"filename" : "$FILE_88x88",
"scale" : "2x",
"role" : "appLauncher",
"subtype" : "40mm"
},
{
"size" : "50x50",
"idiom" : "watch",
"filename" : "$FILE_100x100",
"scale" : "2x",
"role" : "appLauncher",
"subtype" : "44mm"
},
{
"size" : "86x86",
"idiom" : "watch",
"filename" : "$FILE_172x172",
"scale" : "2x",
"role" : "quickLook",
"subtype" : "38mm"
},
{
"size" : "98x98",
"idiom" : "watch",
"filename" : "$FILE_196x196",
"scale" : "2x",
"role" : "quickLook",
"subtype" : "42mm"
},
{
"size" : "108x108",
"idiom" : "watch",
"filename" : "$FILE_216x216",
"scale" : "2x",
"role" : "quickLook",
"subtype" : "44mm"
},
{
"size" : "1024x1024",
"idiom" : "watch-marketing",
"filename" : "$FILE_1024x1024",
"scale" : "1x"
}
],
"info" : {
"version" : 1,
"author" : "ios_icon_set"
}
}
EOF
exit 1
fi
# System icons
# Create a colorized copy of the original file, if needed.
if [ $COLORIZE -ne 0 ]; then
FILE_NAME_COLORIZED=$FILE_NAME"_colorized"
convert $IMG -alpha off -fill $COLOR -colorize 100% -alpha on $FILE_NAME_COLORIZED$EXT
FILE_NAME=$FILE_NAME_COLORIZED
fi
# Trim whitespace if needed, otherwise copy the file.
FILE_COPY=$FILE_NAME"_copy"$EXT
FILE=$FILE_NAME$EXT
if [ $TRIM -ne 0 ]; then
convert $FILE -trim $FILE_COPY
else
cp $FILE $FILE_COPY
fi
MDPI=$DIR"drawable-mdpi"
HDPI=$DIR"drawable-hdpi"
XHDPI=$DIR"drawable-xhdpi"
XXHDPI=$DIR"drawable-xxhdpi"
XXXHDPI=$DIR"drawable-xxxhdpi"
gen_android() {
convert $FILE_COPY -resize $1 $MDPI/$IMG
convert $FILE_COPY -resize $2 $HDPI/$IMG
convert $FILE_COPY -resize $3 $XHDPI/$IMG
convert $FILE_COPY -resize $4 $XXHDPI/$IMG
convert $FILE_COPY -resize $5 $XXXHDPI/$IMG
}
handle_android() {
makeDir $MDPI
makeDir $HDPI
makeDir $XHDPI
makeDir $XXHDPI
makeDir $XXXHDPI
# Material Action, Notification
if [ $TYPE = action -a $MATERIAL -ne 0 -o $TYPE = notification ]; then
gen_android 24x24 36x36 48x48 72x72 96x96
fi
# Normal Action
if [ $TYPE = action -a $MATERIAL = 0 ]; then
gen_android 32x32 48x48 64x64 96x96 128x128
fi
# Small
if [ $TYPE = small ]; then
gen_android 16x16 24x24 32x32 48x48 64x64
fi
# Custom
if [ $TYPE = custom ]; then
gen_android $SIZEx$SIZE \
`expr $SIZE \\* 3 / 2`x`expr $SIZE \\* 3 / 2` \
`expr $SIZE \\* 2`x`expr $SIZE \\* 2` \
`expr $SIZE \\* 3`x`expr $SIZE \\* 3` \
`expr $SIZE \\* 4`x`expr $SIZE \\* 4`
fi
}
gen_ios_icon() {
FILE_1X=$ORIGINAL_FILE_NAME"_1x"$EXT
FILE_2X=$ORIGINAL_FILE_NAME"_2x"$EXT
FILE_3X=$ORIGINAL_FILE_NAME"_3x"$EXT
convert $FILE_COPY -resize $2 $1/$FILE_1X
convert $FILE_COPY -resize $3 $1/$FILE_2X
convert $FILE_COPY -resize $4 $1/$FILE_3X
}
handle_ios() {
# Create .imageset directory.
DIR=$DIR$ORIGINAL_FILE_NAME".imageset"
makeDir $DIR
# TabBar
if [ $TYPE = tabbar ]; then
gen_ios_icon $DIR 25x25 50x50 75x75
fi
# Toolbar
if [ $TYPE = toolbar ]; then
gen_ios_icon $DIR 22x22 44x44 66x66
fi
# UITableViewCell
if [ $TYPE = tableviewcell ]; then
gen_ios_icon $DIR 25x25 50x50 75x75
fi
# Custom
if [ $TYPE = custom ]; then
gen_ios_icon $DIR $SIZEx$SIZE `expr $SIZE \\* 2`x`expr $SIZE \\* 2` `expr $SIZE \\* 3`x`expr $SIZE \\* 3`
fi
# Create Contents.json file.
cat << EOF > $DIR/Contents.json
{
"images" : [
{
"idiom" : "universal",
"filename" : "$FILE_1X",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "$FILE_2X",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "$FILE_3X",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "ios_icon_set"
}
}
EOF
}
if [ $PLATFORM = android ]; then
handle_android
fi
if [ $PLATFORM = ios ]; then
handle_ios
fi
# Delete temporary files
rm $FILE_COPY
if [ $COLORIZE -ne 0 ]; then
rm $FILE_NAME_COLORIZED$EXT
fi