Last active
February 23, 2018 03:32
-
-
Save curtisbelt/b403b6b86604e7f68773fa248d6cc568 to your computer and use it in GitHub Desktop.
Bulk Compress JPG/PNG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Setup variable extra_args with -newer argument if our tracking file exists | |
# Find all files with extension jpg/jpeg/png, case insensitive | |
# For jpg/jpeg, execute jpegoptim | |
# For png, execute pngquant | |
# Log output to compress-all-images.log | |
# https://stackoverflow.com/questions/38965254/jpegoptim-save-shell-last-run-and-check-files-or-folders-after-this-date | |
# https://unix.stackexchange.com/questions/15308/how-to-use-find-command-to-search-for-multiple-extensions | |
# https://unix.stackexchange.com/questions/196399/how-to-batch-resize-all-images-in-a-folder-including-subfolders | |
# https://stackoverflow.com/questions/19784690/when-tee-command-redirect-to-subshell-the-last-two-lines-missing | |
# https://stackoverflow.com/questions/229551/string-contains-in-bash | |
compress_images () { | |
echo "Processing File: $1" | |
if [[ "$1" == *".png" ]]; then | |
pngquant -s 4 --skip-if-larger -Q 60-70 -f --ext .png -v "$@" | |
else | |
jpegoptim -m80 -o -t -v --all-progressive "$@" | |
fi | |
echo "End Processing" | |
} | |
export -f compress_images | |
extra_args=( ) | |
[[ -e .images-last-compressed ]] && extra_args=( -newer .images-last-compressed ) | |
{ | |
find ./ -type f \( -iname \*.jpg -o -iname \*.jpeg -o -iname \*.png \) "${extra_args[@]}" \ | |
-exec bash -c 'compress_images "{}"' \; | |
} 2>&1 | awk '{ print strftime("%Y-%m-%d %H:%M:%S: " ), $0; fflush(); }' | tee -a compress-all-images.log | |
touch .images-last-compressed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment