Skip to content

Instantly share code, notes, and snippets.

@AndrewRayCode
Created July 2, 2013 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewRayCode/5906379 to your computer and use it in GitHub Desktop.
Save AndrewRayCode/5906379 to your computer and use it in GitHub Desktop.
#!/bin/sh
compression=80
hasExif=`which exiftool`
pngs=`git status -s | grep .png | awk '{print $2}'`
arr=($pngs)
if [ -n "$arr" ]; then
echo "Compressing png images by $compression%..."
for png in "${arr[@]}"
do
oldSize=`exiftool -FileSize $png | awk '{print $4$5}'`
pngquant --quality=80 --ext=.png --force $png
newSize=`exiftool -FileSize $png | awk '{print $4$5}'`
git add $png
echo " Compressed $png from $oldSize to $newSize"
done
fi
exit 0
@AndrewRayCode
Copy link
Author

Git pre commit hook to compress (modified) png files, and automatically add them to the commit. This assumes you are exporting the data from a PSD / image editing program every time you make a change, so you will not re-compress a compressed image.

put the gist in .git/hooks/pre-commit

brew install pngquant
brew install exiftool
chmod +x .git/hooks/pre-commit

Remember, you cannot commit git hooks, so you must add this to any machine you wish to have it on (yes, git is dumb)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment