-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompress_image.sh
22 lines (22 loc) · 975 Bytes
/
compress_image.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) -print0 |
while IFS= read -r -d $'\0' file; do
if [[ $(stat -c "%s" "$file") -gt 1048576 ]]; then
echo "Compressing $file"
ext="${file##*.}"
width=$(identify -format "%w" "$file")
height=$(identify -format "%h" "$file")
if [[ "$width" -gt 2000 || "$height" -gt 2000 ]]; then
if [[ "$ext" == "png" ]]; then
magick convert "$file" -resize 50% "$file"
else
magick convert "$file" -resize 50% -sampling-factor 4:2:0 -strip -quality 85% "${file%.*}_compressed.""$ext" && rm "$file" && mv "${file%.*}_compressed.""$ext" "$file"
fi
else
if [[ "$ext" == "png" ]]; then
optipng -o7 "$file"
else
magick convert "$file" -sampling-factor 4:2:0 -strip -quality 85% "${file%.*}_compressed.""$ext" && rm "$file" && mv "${file%.*}_compressed.""$ext" "$file"
fi
fi
fi
done