-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress-screenshot.sh
executable file
·78 lines (58 loc) · 2.29 KB
/
compress-screenshot.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
#!/bin/bash
SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# human-readable conditions by qzb (https://github.com/qzb/is.sh)
source "$SCRIPT_DIR/is"
MAX_ORIGINAL_ALLOWED_SIZE_KB=500
QUALITY=94
function echoerr {
printf "%s\n" "$*" >&2;
}
function file_size_kb { FILE_PATH="$1"
du -k "$FILE_PATH" | cut -f1
}
FILE_REL_PATH="$1"
if is empty "$FILE_REL_PATH"; then
echoerr "Usage: $SCRIPT_NAME file_name"
exit 10
fi
if is not file "$FILE_REL_PATH"; then
echoerr "Error: \"$FILE_REL_PATH\" is not file"
exit 15
fi
FILE_ABS_PATH=$(readlink -f "$FILE_REL_PATH")
FILE_DIR_PATH=$(dirname "$FILE_ABS_PATH")
FILE_NAME_WITH_EXT=$(basename "$FILE_REL_PATH")
FILE_EXTENSION=$(echo "$FILE_NAME_WITH_EXT" | rev | cut -f 1 -d '.' | rev)
FILE_NAME_NO_EXT=$(echo "$FILE_NAME_WITH_EXT" | sed -e s/\.${FILE_EXTENSION}$//g)
if [[ "$FILE_EXTENSION" != 'png' ]]; then
echoerr "Error: extension .$FILE_EXTENSION is not supported"
exit 20
fi
FILE_SIZE_KB=$(file_size_kb "$FILE_ABS_PATH")
if [ "$FILE_SIZE_KB" -le "$MAX_ORIGINAL_ALLOWED_SIZE_KB" ]; then
echo "Skipping compression: file size is just ${FILE_SIZE_KB}kb (<= ${MAX_ORIGINAL_ALLOWED_SIZE_KB}kb)"
exit
fi
# if ! command -v cwebp &> /dev/null ;then
# echoerr "cwebp command could not be found. Install webp package first"
# exit 30
# fi
if ! command -v mozjpeg &> /dev/null ;then
echoerr "mozjpeg command could not be found. Do \"npm install --global mozjpeg\" first"
exit 30
fi
OUTPUT_PATH="$FILE_DIR_PATH/$FILE_NAME_NO_EXT.jpg"
# cwebp -q 100 "$FILE_ABS_PATH" -o "$OUTPUT_PATH" &> /dev/null
mozjpeg -quality "$QUALITY" -outfile "$OUTPUT_PATH" "$FILE_ABS_PATH"
OUTPUT_FILE_SIZE_KB=$(file_size_kb "$OUTPUT_PATH")
if [ "$OUTPUT_FILE_SIZE_KB" -lt "$FILE_SIZE_KB" ]; then
PERCENT_DELTA_RAW=$(echo "($FILE_SIZE_KB-$OUTPUT_FILE_SIZE_KB)/$FILE_SIZE_KB*100" | bc -l)
PERCENT_DELTA=$(printf %.0f "$PERCENT_DELTA_RAW")
# echo "compression png -> webp: (-${PERCENT_DELTA}%: ${FILE_SIZE_KB}kb -> ${OUTPUT_FILE_SIZE_KB}kb)"
echo "compression png -> jpg: (-${PERCENT_DELTA}%: ${FILE_SIZE_KB}kb -> ${OUTPUT_FILE_SIZE_KB}kb)"
rm "$FILE_ABS_PATH" &> /dev/null
else
echo "Compression was not useful. Deleting output file"
rm "$OUTPUT_PATH" &> /dev/null
fi