forked from ricardodorosario/batch-image-resizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch-image-resizer.sh
executable file
·55 lines (47 loc) · 1.3 KB
/
batch-image-resizer.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
#!/bin/bash
#Example: ./batch-image-resizer.sh -d /path/to/root/directory -s 1000
helpFunction()
{
printf "\n"
printf "Usage: $0 -d /path/to/root/dir -s maxSize"
printf "\n"
exit 1
}
while getopts "d:s:q:" opt
do
case "$opt" in
d ) directory="$OPTARG" ;;
s ) max="$OPTARG" ;;
q ) quality="$OPTARG" ;;
? ) helpFunction ;;
esac
done
if [ -z "$directory" ] || [ -z "$max" ]
then
printf "Some of the parameters are empty";
helpFunction
fi
if [ -z "$quality" ]
then
printf "\nQuality not provided, defaut to 90\n";
quality=90
fi
# the extensions
extensions=".*\(jpg\|png\|jpeg\)$"
find "$directory" -regex "$extensions" |
while read dir
do
width=$(identify -format "%w" "$dir")
height=$(identify -format "%h" "$dir")
if [ \( $width -gt $max \) -o \( $height -gt $max \) ]
then
printf "\n\nBIGGER THAN EXPECTED\nBefore: $width x $height\n$dir\n"
permission=$(find "$dir" -printf "%u:%g")
mogrify -geometry "$max" -quality "$quality" "$dir"
chown $permission "$dir"
echo 'After:' $(identify -format "%w x %h" "$dir")
else
printf "\nBELOW THRESHOLD\nResolution: $width x $height\n$dir\nfile not modified\n\n"
fi
done
printf "\nALL DONE\n$directory directory fully parsed and processed\n\n"