Skip to content

Instantly share code, notes, and snippets.

@martinlindhe
Created December 12, 2015 01:46
Show Gist options
  • Save martinlindhe/a20edb0092972a252d38 to your computer and use it in GitHub Desktop.
Save martinlindhe/a20edb0092972a252d38 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# NOTE: this only works on OSX due to the syntax of the stat command
#
# brew install jpeg jpegoptim
inFile=$1
if [ -z $inFile ]; then
echo "Syntax: $0 filename"
exit
fi
inFileSize=$(stat -f%z "$inFile")
tempFile=$inFile.tempFile.jpg
#echo "Shrinking $inFile ..."
if [ ! -e $inFile ]; then
echo "$inFile dont exist"
exit
fi
# strips useless stuff (like comments and timestamps) and optimizes file without changing compression
jpegtran -v -optimize -copy none -outfile $tempFile $inFile 2> /dev/null
if [ $? -ne 0 ]; then
echo "Error occured while processing $inFile"
exit
fi
outFileSize=$(stat -f%z "$tempFile")
diffSize=$(($inFileSize - $outFileSize))
if [ $diffSize -lt 1 ]; then
# echo "Error: resulting $inFile is not smaller than the original ($diffSize)"
rm $tempFile
exit
fi
echo "$inFile was $inFileSize bytes, shrank to $outFileSize. saved $diffSize bytes"
rm $inFile
mv $tempFile $inFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment