~jbauer/dotfiles

6ea121be0a9cd155d5de5194d94eb53e4e531d31 — Jake Bauer a month ago 77e39a6
Add script to easily optimize images
1 files changed, 38 insertions(+), 0 deletions(-)

A .local/bin/optimizeimg
A .local/bin/optimizeimg => .local/bin/optimizeimg +38 -0
@@ 0,0 1,38 @@
#!/bin/sh

optimizejpg()
{
	convert \
	-sampling-factor 4:2:0 \
	-strip -quality 85 \
	-interlace JPEG \
	-colorspace RGB \
	"$file" \
	"$filename"-optimized.jpg
	exiftool -all= "$filename"-optimized.jpg
}

optimizepng()
{
	if [ ! -x $(command -v optipng) ]; then
		echo "Optimizing PNGs requires the optipng program."
		exit 1
	fi
	optipng -out "$filename"-optimized.png "$file"
	exiftool -all= "$filename"-optimized.png
}

for file in "$@"; do
	if [ -f "$file" ]; then
		case "$file" in
			*.jpg) filename=$(basename "$file" .jpg); optimizejpg ;;
			*.jpeg) filename=$(basename "$file" .jpeg); optimizejpg ;;
			*.png) filename=$(basename "$file" .png); optimizepng ;;
			*)
				filename=$(basename "$file" .png)
				echo "The provided file is not a JPG or PNG, it will be converted to a PNG and optimized with optipng."
				optimizepng
				;;
		esac
	fi
done