Skip to content

Instantly share code, notes, and snippets.

@sorbing
Created May 23, 2017 09:00
Show Gist options
  • Save sorbing/8f92074d9a52a168e65b75941d2ed062 to your computer and use it in GitHub Desktop.
Save sorbing/8f92074d9a52a168e65b75941d2ed062 to your computer and use it in GitHub Desktop.
Images optimize bash script
#!/bin/bash
# Require:
# sudo apt-get install jpegoptim pngnq advancecomp
FILE_PATH=$1
if [[ -z $FILE_PATH ]]; then
echo 'Не указан каталог или изображение!'
exit -1;
fi
FILE_PATH=$(echo $FILE_PATH | sed "s/file:\/\///g")
ABSOLUTE_PATH="$(cd "$(dirname "$FILE_PATH")"; pwd)/$(basename "$FILE_PATH")"
IMAGES="$ABSOLUTE_PATH"
if [[ -d $ABSOLUTE_PATH ]]; then
IMAGES=$(find $ABSOLUTE_PATH -type f -iname '*.png' -or -iname '*.jpg')
fi
for IMG in $IMAGES; do
if [[ $IMG =~ \.png$ ]]; then
new_name=${IMG%'.png'}'-nq8.png'
echo "$IMG"
pngnq -f "$IMG"
mv -f $new_name $IMG
advpng -z4 -q $IMG
chmod 666 $IMG
fi
if [[ $IMG =~ \.jpg$ ]]; then
echo "$IMG"
jpegoptim -q --strip-all "$IMG"
chmod 666 $IMG
fi
done
notify-send -t 100 "Оптимизация завершена" "Убедитесь, что изображения оптимизированы"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment