-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpgpngoptimizer
executable file
·62 lines (56 loc) · 1.6 KB
/
jpgpngoptimizer
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
#!/bin/bash
FILE="$1"
TMPDIR="/tmp"
TMPFILE="fake"
if [ $# -eq 0 ]
then
echo "No arguments"
echo "Usage: $0 filename"
exit 1
fi
if [[ -e "$FILE" && ${FILE: -4} != ".tmp" ]]
then
TYPE=$(file -b --mime-type "$FILE")
if [[ $TYPE == "image/jpeg" || $TYPE == "image/png" ]]
then
FILENAME=$(basename "$FILE")
TMPFILE="${TMPDIR}/${FILENAME}.tmp"
echo "Copying $FILE to $TMPFILE"
cp -f "$FILE" "$TMPFILE"
fi
else
echo "Skip this file: $FILE"
exit 1
fi
case $TYPE in
"image/jpeg")
echo "New jpeg file: $FILE"
echo "Start processing"
jpegoptim -f --strip-all "$TMPFILE" && \
echo "Success optimize" && \
mv -f "$TMPFILE" "${FILE}".tmp && \
mv -f "${FILE}".tmp "$FILE" && \
chmod 0644 "$FILE" && \
echo "File renamed back from $TMPFILE to $FILE"
;;
"image/png")
echo "New png file: $FILE"
optipng -force -o7 "$TMPFILE" && \
advpng -z4 "$TMPFILE" && \
pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP \
-rem sRGB -rem time "$TMPFILE" "${TMPFILE}".tmp && \
mv -f "${TMPFILE}".tmp "${FILE}".tmp && \
mv -f "${FILE}".tmp "$FILE" && \
chmod 0644 "$FILE" && \
echo "Renamed back from ${TMPFILE}.tmp to $FILE"
;;
*)
rm -f "$TMPFILE"
exit 1
;;
esac
if [ -e "$TMPFILE" ]
then
rm -f "$TMPFILE" && echo "Temporary file $TMPFILE deleted"
fi
exit 0