forked from bianjp/image-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify.sh
executable file
·110 lines (99 loc) · 2.55 KB
/
minify.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
#!/usr/bin/bash
# Minify images using TinyPNG
API_KEY=`cat "$(dirname $0)"/api.key`
API_URL=https://api.tinify.com/shrink
# directory to save minified files
output_dir=
# files to minify
files=()
show_help() {
echo -e \
"Minify images using TinyPNG. Replace original files by default.
Support only png, jpg.
Usage: ./minify_image.sh [option] [file/directory]...
Options:
-o [Output Dir] save minified images in the specified directory
-h, --help display this help and exit
"
}
# parse and check output directory. Create if necessary
parse_output_dir() {
if [[ "$1" = '-o' ]]; then
# make sure end with '/'
output_dir=${2%/}/
if [[ -e "$output_dir" ]]; then
if [[ ! -d "$output_dir" ]]; then
echo "Error: $output_dir is not a directory"
exit 1
elif [[ ! -w "$output_dir" ]]; then
echo "Error: $output_dir is not writable"
exit 1
fi
else
mkdir "$output_dir" || exit 1
fi
fi
}
# parse files and directory to an array of file. Exit if file or directory not exist
parse_files() {
start=0
if [[ -n "$output_dir" ]]; then
start=2
fi
args=("$@")
for (( i = $start; i <= $#; i++ )); do
path=${args[$i]}
if [[ -f "$path" ]]; then
files+=($path)
elif [[ -d "$path" ]]; then
for file in "`find \"$path\" -type f -regex '.*\.\(png\|jpg\)'`"; do
if [[ -n "$file" ]]; then
files+=("$file")
fi
done
fi
done
}
# minify one image
minify_image() {
local file=$1
local response=`curl $API_URL -s --user api:$API_KEY --data-binary @"$file"`
if [[ "$response" = *"\"error\""* ]]; then
message=`echo "$response" | sed 's#.*"message":"\([^"]*\)".*#\1#'`
if [[ -n "$message" ]]; then
echo "$file: $message"
else
echo "$file: $response"
fi
else
local url=`echo "$response" | sed 's#.*"url":"\([^"]*\)".*#\1#'`
if [[ -n "$output_dir" ]]; then
local output_path="$output_dir$file"
else
local output_path=$file
fi
curl $url -sSo "$output_path" --user api:$API_KEY
if [[ "$?" = "0" ]]; then
echo "$file: success"
else
echo "$file: failed to download file"
fi
fi
}
if [[ "$#" = "0" || "$1" = "--help" || "$1" = "-h" ]]; then
show_help
exit
elif [[ -e "$API_KEY" ]]; then
echo "An api key is needed"
fi
parse_output_dir $@
parse_files $@
export API_KEY
export API_URL
export output_dir
export -f minify_image
if [[ "${#files[@]}" -lt "1" ]]; then
echo "No file to minify"
else
echo ${files[@]} | xargs -r -n1 -P10 bash -c 'minify_image "$@"' _{}
fi