Skip to content

Instantly share code, notes, and snippets.

@mrnonz
Last active August 2, 2018 05:23
Show Gist options
  • Save mrnonz/4f872ee491999822fac3160b6c840929 to your computer and use it in GitHub Desktop.
Save mrnonz/4f872ee491999822fac3160b6c840929 to your computer and use it in GitHub Desktop.
Recursively optimize all PNG and JPG files wherever they are from the script position and inner ( based on OPTING, PNGCRUSH, ADVANCECOMP and JPEGOPTIM )
#!/bin/bash
find . -type f -name "*.png" -o -name "*.PNG" | xargs optipng -nb -nc
find . -type f -name "*.png" -o -name "*.PNG" | xargs advpng -z4
find . -type f -name "*.png" -o -name "*.PNG" | xargs pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time -ow
find . -type f -name "*.jpg" -o -name "*.JPG" | xargs jpegoptim -f --strip-all
## Log to file
# find . -type f -iname "*.png" -print0 | xargs -I {} -0 optipng -o5 -quiet -keep -preserve -log optipng.log "{}"
@mrnonz
Copy link
Author

mrnonz commented Jul 11, 2018

If you wish to compress some file in 5 minute with 70% max quality just type like below

find . -mmin -5 -type f -name "*.jpg" -o -name "*.JPG" | xargs jpegoptim -m70 -f --strip-all -p

@mrnonz
Copy link
Author

mrnonz commented Jul 11, 2018

Crontab for run every 5th minutes.

*/5 * * * * find /path/to/images/directory/ -mmin -5 -type f -name "*.jpg" -o -name "*.JPG" | xargs jpegoptim -m70 -f --strip-all -p \; > /dev/null

@mrnonz
Copy link
Author

mrnonz commented Aug 2, 2018

How to resize image with same quality

find . -type f -size +1M | xargs -i{} convert -resize 50% {} {}

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