JPEG compression script

This commit is contained in:
Brandon Rozek 2022-06-12 12:06:50 -04:00
parent bffd80ae42
commit cc4bdc9d67

37
jpg4web.bash Executable file
View file

@ -0,0 +1,37 @@
#!/bin/bash
# 800px is the width of the content on my site
WIDTH=800
set -o errexit
set -o nounset
set -o pipefail
show_usage() {
echo "Usage: jpg4web.bash [imagefile]"
exit 1
}
# Check argument count
if [ "$#" -ne 1 ]; then
show_usage
fi
# Check that it ends in .jpg
if [[ "$1" != *.jpg ]]; then
echo "Argument [imagefile] must end in .jpg"
show_usage
fi
# Check that relevant commands exist
if ! command -v mogrify > /dev/null; then
echo "Command magick not found. Exiting..."
fi
if ! command -v jpegoptim > /dev/null; then
echo "Command jpegoptim not found. Exiting..."
fi
cp "$1" "$1.bak"
mogrify -resize "$WIDTH" "$1"
jpegoptim "$1"