-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg-optimize.sh
executable file
·65 lines (56 loc) · 1.88 KB
/
img-optimize.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
#!/bin/bash
# Check if imagemagick is installed
hash mogrify 2>/dev/null || {
echo "Error: imagemagick not found"
exit 1
}
# Check if pngquant is installed
hash pngquant 2>/dev/null || {
echo "Error: pngquant not found"
exit 1
}
# Initialize variables to hold the values of the "path" and "mtime" options
path=""
mtime=""
# Process command-line options
while [ "$1" != "" ]; do
case "$1" in
-p|--path) path="$2"; shift;;
-t|--mtime) mtime="$2"; shift;;
*) echo "Error: Unknown option $1"; exit 1;;
esac
shift
done
# exit if a path wasn't provided
[ -z "$path" ] && {
echo "Error: No path provided"
exit 1
}
# Set defaults
mtime=${mtime:-"-7"}
# Find all regular files in the specified directory and its subdirectories that are not hidden
# and have been modified within the specified number of days.
# Exclude hidden files and directories (those that start with a dot).
# Only include files with the .jpg or .jpeg extension.
# Print the names of the matching files, separated by null characters.
# Pipe the names to xargs, which will pass them as arguments to jpegoptim.
find "$path" \
-not -path '*/.*' \
-type f \
-mtime $mtime \
\( -iname '*.jpg' -o -iname '*.jpeg' \) \
-print0 \
| xargs -0 jpegoptim -q --strip-all --max=82
# Find all regular files in the specified directory and its subdirectories that are not hidden
# and have been modified within the specified number of days.
# Exclude hidden files and directories (those that start with a dot).
# Only include files with the .png extension.
# Print the names of the matching files, separated by null characters.
# Pipe the names to xargs, which will pass them as arguments to pngquant.
find "$path" \
-not -path '*/.*' \
-type f \
-mtime $mtime \
-iname '*.png' \
-print0 \
| xargs -0 pngquant -q --force --ext .png --quality=80-95 --strip --skip-if-larger