-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
assets_generator.sh
executable file
·150 lines (131 loc) · 3.88 KB
/
assets_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
#!/bin/bash
function usage() {
echo "usage: $0 <image-filename> [--output=output-directory]";
exit 1;
}
image=""
output="."
width="-1"
height="-1"
density="-1"
outputExt=""
platform="all"
while [ $# -gt 0 ]; do
case "$1" in
-o|--output=*)
output="$2"
;;
-w|--width=*)
width="$2"
;;
-e|--ext=*)
outputExt="$2"
;;
-h|--height=*)
height="$2"
;;
-d|--density=*)
density="$2"
;;
-p|--platform=*)
platform="$2"
;;
*)
image="$1"
esac
shift
done
[ "$image" ] || usage
nameWithExt=${image##*/}
name=${nameWithExt%.*}
extension="${nameWithExt##*.}"
devices=iOS,Android #,windows-phone,bada,bada-wac,blackberry,webos
eval mkdir -p "$output/{$devices}"
# Show the user some progress by outputing all commands being run.
# set -x
# Setting IFS (input field separator) value as ","
IFS='x'
# Reading the split string into array
if [ "$extension" = "svg" ]; then
if [ "$outputExt" = "" ]; then
outputExt="png"
fi
TEMP_DIR=`mktemp -d`
echo "converting svg image $width $height"
if [ "$width" -gt "0" ] && [ "$height" -gt "0" ]; then
rsvg-convert -h $height -w $width "$image" > $TEMP_DIR/$name.$outputExt
elif [ "$height" -gt "0" ]; then
rsvg-convert -h $height "$image" > $TEMP_DIR/$name.$outputExt
elif [ "$width" -gt "0" ]; then
rsvg-convert -w $width "$image" > $TEMP_DIR/$name.$outputExt
fi
image="$TEMP_DIR/$name.$outputExt"
echo "svgGeneratedImage $image"
fi
echo "image $image"
imageSize=$(convert "$image" -format "%wx%h" info:)
read -ra sizes <<< "$imageSize"
echo "width $width"
echo "height $height"
echo "image $image"
if [ "$width" -gt "0" ]; then
echo "hanlding width $width"
echo "sizes[0] ${sizes[0]}"
echo "sizes[1] ${sizes[1]}"
ratio="$(echo "(`expr ${sizes[0]}` / `expr ${sizes[1]}`)" | bc -l)"
echo "ratio $ratio"
sizes[0]=$width
sizes[1]="$(echo "(`expr $width` / `expr $ratio`+0.5)/1" | bc )"
fi
if [ "$height" -gt "0" ]; then
echo "hanlding height $height"
echo "sizes[0] ${sizes[0]}"
echo "sizes[1] ${sizes[1]}"
ratio="$(echo "(`expr ${sizes[0]}` / `expr ${sizes[1]}`)" | bc -l)"
echo "ratio $ratio"
sizes[1]=$height
sizes[0]="$(echo "(`expr $height` * `expr $ratio`+0.5)/1" | bc )"
echo "width ${sizes[0]}"
echo "height ${sizes[1]}"
fi
function convertImage() {
local factor="$1"
local output="$2"
convert "$image" -background none -density "$density" -resize "$(getSize $factor)" "$output"
# opti "$output"
}
function getSize() {
local factor="$1"
echo "$(echo "(`expr ${sizes[0]}`* $factor+0.5)/1" | bc)x$(echo "(`expr ${sizes[1]}`* $factor+0.5)/1" | bc)"
}
function opti() {
optipng -nb -nc "$*";
advpng -z4 "$*";
pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time -ow "$*";
}
function androidConvertIcon() {
echo "androidConvertIcon"
drawables=drawable,drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-xxhdpi,drawable-xxxhdpi
eval mkdir -p "$output/Android/src/main/res/{$drawables}"
convertImage "0.1875" "$output/Android/src/main/res/drawable-ldpi/$name.$outputExt"
convertImage "0.25" "$output/Android/src/main/res/drawable-mdpi/$name.$outputExt"
convertImage "0.375" "$output/Android/src/main/res/drawable-hdpi/$name.$outputExt"
convertImage "0.5" "$output/Android/src/main/res/drawable-xhdpi/$name.$outputExt"
convertImage "0.75" "$output/Android/src/main/res/drawable-xxhdpi/$name.$outputExt"
convertImage "1" "$output/Android/src/main/res/drawable-xxxhdpi/$name.$outputExt"
}
function iosConvertIcon() {
echo "iosConvertIcon $output"
convertImage "0.33" "$output/iOS/$name.$outputExt"
convertImage "0.66" "$output/iOS/$name@2x.$outputExt"
convertImage "1" "$output/iOS/$name@3x.$outputExt"
}
if [ "$outputExt" = "" ]; then
outputExt="$extension"
fi
if [[ "$platform" == "all" || "$platform" == "android" ]]; then
androidConvertIcon
fi
if [[ "$platform" == "all" || "$platform" == "ios" ]]; then
iosConvertIcon
fi