From cc4bdc9d677eca2240bcf093b0889a90e374a271 Mon Sep 17 00:00:00 2001 From: Brandon Rozek Date: Sun, 12 Jun 2022 12:06:50 -0400 Subject: [PATCH] JPEG compression script --- jpg4web.bash | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 jpg4web.bash diff --git a/jpg4web.bash b/jpg4web.bash new file mode 100755 index 0000000..d04c76c --- /dev/null +++ b/jpg4web.bash @@ -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" +