-
Notifications
You must be signed in to change notification settings - Fork 0
/
icon_fits
executable file
·563 lines (462 loc) · 10.5 KB
/
icon_fits
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
552
553
554
555
556
557
558
559
560
561
562
563
#!/usr/bin/env bash
#
# App icon fitter
# A tool for creating images for application icons.
# Create by Poplax [linjiang9999<at>gmail.com]
# Date 2018-02-06
#
set -eu
declare -r TOOLNAME='AppIconFitter'
declare -r VERSION='0.1.0'
declare -r EXTENSION='.png'
err() {
local red="\033[1;31m"
local normal="\033[0m"
local err_text
err_text="$1"
echo -e "<${red}Error${normal}> ${err_text}" >&2
}
message() {
local green="\033[1;32m"
local normal="\033[0m"
local message_text
message_text="$1"
echo -e "[${green}Message${normal}] ${message_text}"
}
lowcase() {
local word
word="$1"
echo "${word}" | tr '[:upper:]' '[:lower:]'
}
make_dir() {
local target_dir
target_dir="$1"
if [[ -z "${target_dir}" ]]; then
err "Target dir can't be empty."
exit -1
fi
if [[ ! -d "${target_dir}" ]]; then
mkdir -p "${target_dir}"
fi
}
check_option() {
local opt
local first
opt="$1"
first="$2"
if [[ ${first:0:1} == '-' ]]; then
err "\nOption -${opt}\nRequires argument error: can't start with '-'. "
exit -1
fi
}
usage() {
cat <<USAGE_DOC
Version: ${TOOLNAME}-v${VERSION}
Copyright: © 2018 Poplax [linjiang9999<at>gmail.com]
License: MIT <https://opensource.org/licenses/MIT>
Usage:
$0 [OPTION ...]
-f Image of App icon file, recommand size '1024x1024' px, Support png,jpeg,tiff
-o Output directory. default is current directory 'output'.
-t Specify subtitle text in the icon, default is none.
-k Auto create all size of App icon. note: only support 'iOS' platform, this version.
-s Specify size, note: Only create an icon of the specified size.
-c Compress files.
-v Software current version.
-h Help.
Example :
+ Resize 120(px) of the icon(gray_1024.png), add subtitle : 'DemoApp' as watermark, create one icon to the 'output' directory:
1>
$0 -s 120 -f gray_1024.png -o iconFitResize -t "DemoApp"
2>
$0 -c -s 120 -o iconFitResize -t "DemoApp" gray_1024.png
+ create all size of the platform(default: iOS) support.
1>
$0 -k -f blue_1024.png -o iconFit
2>
$0 -co iconFitCompress blue_1024.png
USAGE_DOC
}
# Create watermark.
make_watermark() {
# message "make watermark."
local subtitle
local image_file
subtitle="$1"
image_file="$2"
local image_watermark="wmark_${image_file}"
convert "${image_file}" -font GeorgiaB -pointsize 120 \
-draw "gravity south fill white text 0,0 '${subtitle}' fill DarkGray text 0,-2 '${subtitle}'" \
"${image_watermark}"
if [[ -f "${image_watermark}" ]]; then
echo "${image_watermark}"
fi
}
# Make image.
make_image() {
local size
local input_file
local output_dir
local filename="default_file${EXTENSION}"
size="$1"
input_file="$2"
output_dir="$3"
if [[ "$#" -ge 4 ]]; then
filename="$4"
fi
if [[ "${size}" -lt 10 || "${size}" -gt 2048 ]]; then
err "Out of image size, min = 10, max = 2048."
exit -1
fi
local output_file="${output_dir}/${filename}"
if convert "${input_file}" -resize ${size} "${output_file}"; then
message "Created ${output_file} ."
fi
}
compress_files() {
local file_ext
local file_dir
local file_name
file_ext="$1"
file_dir="$2"
[[ "$#" -ge 3 ]] &&
file_name="$3"
if [[ "${file_ext}" == ".png" ]]; then
message "Compress start ..."
if [[ -n "${file_name}" ]]; then
pngcrush -rem allb -brute -nofilecheck -blacken -reduce -ow ${file_name}
else
find ${file_dir} -name "*${file_ext}" |
xargs -I {} pngcrush -rem allb -brute -nofilecheck -blacken -reduce -ow {}
fi
message "Compress done ."
else
# @TODO: Compress .jpg ...
err "Compress Not support ${file_ext} yet."
fi
}
make_ios_icons() {
local origin_file
local output_dir
origin_file="$1"
output_dir="$2"
# Sizes value
local app_store_1x="1024"
make_image "${app_store_1x}" "${origin_file}" "${output_dir}" "iTunesArtwork${EXTENSION}"
local suffix
local iphone_3x="20 29 40 60"
for m_size in ${iphone_3x}; do
suffix=''
for i in {1..3}; do
[[ $i -gt 1 ]] && suffix="@${i}x"
make_image "$((${m_size} * ${i}))" "${origin_file}" "${output_dir}" "iphone_${m_size}${suffix}${EXTENSION}"
done
done
local ipad_2x="20 29 40 76"
for m_size in ${ipad_2x}; do
suffix=''
for i in {1..2}; do
[[ $i -gt 1 ]] && suffix="@${i}x"
make_image "$((${m_size} * ${i}))" "${origin_file}" "${output_dir}" "ipad_${m_size}${suffix}${EXTENSION}"
done
done
# iPad Pro
local ipad_pro_size="167"
make_image "${ipad_pro_size}" "${origin_file}" "${output_dir}" "ipad_pro_${ipad_pro_size}${EXTENSION}"
}
make_ios_config() {
local config_file='Contents.json'
local output_dir
output_dir="$1"
cat <<CONFIG_IOS >"${output_dir}/${config_file}"
{
"images" : [
{
"size" : "20x20",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "20x20",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "3x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "3x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "[email protected]",
"scale" : "3x"
},
{
"size" : "20x20",
"idiom" : "ipad",
"filename" : "ipad_20.png",
"scale" : "1x"
},
{
"size" : "20x20",
"idiom" : "ipad",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "ipad_29.png",
"scale" : "1x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "ipad_40.png",
"scale" : "1x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "ipad_76.png",
"scale" : "1x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "83.5x83.5",
"idiom" : "ipad",
"filename" : "ipad_pro_167.png",
"scale" : "2x"
},
{
"size" : "1024x1024",
"idiom" : "ios-marketing",
"filename" : "iTunesArtwork.png",
"scale" : "1x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
CONFIG_IOS
}
# main
main() {
# Check Required.
command -v convert >/dev/null 2>&1 ||
{
cat <<EOF
This Tool Depend On
'ImageMagick' [https://www.imagemagick.org/script/index.php].
OSX:
Homebrew install:
brew install imagemagick
Linux:
yum install:
yum install -y ImageMagick
EOF
exit -1
}
# Check Argument
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
# Default kflag='true' in this version.
local kflag='true'
local sflag=''
local fflag=''
local tflag=''
local oflag=''
local cflag=''
# Default value
local size_value=''
local file_value=''
local text_value=''
local kind_value='ios'
local output_value='output'
local OPTIND
while getopts ":cvhks:f:t:o:" opt; do
case ${opt} in
k)
# Output default image files.
kflag='true'
# TODO: Android.
# kind_value=$(lowcase ${kind_value})
# message "-${opt} ${kind_value}"
;;
s)
# Resize.
sflag='true'
check_option "${opt}" "${OPTARG}"
size_value="${OPTARG}"
local re_int='^[0-9]+$'
if [[ ! "${size_value}" =~ $re_int ]]; then
err "Size option -${opt}, only accept +integer.\nyour input is '${size_value}'."
exit 1
fi
# message "-${opt} ${OPTARG}"
;;
f)
# Icon path.
fflag='true'
check_option "${opt}" "${OPTARG}"
file_value="${OPTARG}"
# message "-${opt} ${OPTARG}"
;;
t)
# Watermark text.
tflag='true'
check_option "${opt}" "${OPTARG}"
text_value="${OPTARG}"
# message "-${opt} ${OPTARG}"
;;
o)
# Output directory.
oflag='true'
check_option "${opt}" "${OPTARG}"
output_value="${OPTARG}"
make_dir ${output_value}
# message "-${opt} ${OPTARG}"
;;
c)
# PNG compress.
cflag='true'
command -v pngcrush >/dev/null 2>&1 ||
{
cat <<EOF
This Tool Depend On
'pngcrush' [https://pmt.sourceforge.io/pngcrush/]
OSX:
Homebrew install:
brew install pngcrush
Linux:
yum install:
yum install -y pngcrush
EOF
exit -1
}
;;
v)
echo "${TOOLNAME}-v${VERSION}"
exit 1
;;
h)
usage
exit 1
;;
:)
err "Option -${OPTARG} requires an argument."
usage
exit 1
;;
?)
err "Undefined Option -${OPTARG}"
usage
exit 1
;;
esac
done
shift $((${OPTIND} - 1))
# Create Logic ----
# 1> Handle file input.
if [[ -z "${fflag}" && "$#" -eq 0 ]]; then
err "No input file given."
usage
exit 1
fi
# file input default value.
local input_file
if [[ -z ${fflag} ]]; then
input_file="$1"
fi
if [[ -z "${file_value}" ]]; then
file_value="${input_file}"
fi
# 2> Handle subtitle text.
if [[ -n "${tflag}" ]]; then
local watermark_file
watermark_file=$(make_watermark "${text_value}" "${file_value}")
if [[ -z "${watermark_file}" ]]; then
err "Test watermark make failed."
exit 1
fi
file_value="${watermark_file}"
fi
# 3> Handle type of input type.
if [[ -n "${sflag}" ]]; then
make_dir "${output_value}"
make_image "${size_value}" "${file_value}" "${output_value}" "resize_${size_value}${EXTENSION}"
# Compress
if [[ -n "${cflag}" ]]; then
compress_files "${EXTENSION}" "${output_value}"
fi
local resize_file="${output_value}/resize_${size_value}${EXTENSION}"
message "Create job done. ${resize_file}"
else
# @TODO: android.
if [[ "${kind_value}" == "ios" ]]; then
output_value="${output_value}/AppIcon.appiconset"
make_dir "${output_value}"
make_ios_icons "${file_value}" "${output_value}"
make_ios_config "${output_value}"
fi
# Compress
if [[ -n "${cflag}" ]]; then
compress_files "${EXTENSION}" "${output_value}"
fi
message "Create job done. you can find files at '${output_value}' directory."
fi
# Temp text image need to delete.
if [[ -n "${tflag}" ]]; then
rm -f ${file_value}
fi
}
main "$@"