The idea is to have a script that runs periodically and compresses image files in specified directories
wp-content/uploads
for WordPress, image/catalog/
in OpenCart etc.You being the developer might be conscious of optimizing images before you upload them, but the clients usually aren’t. In cases like these, you can optimize the images on the server after the client has uploaded them, without having to bother yourself or the client.
It also provides a way to optimize your website images to pass google’s pagerank
1FREQUENCY='daily' # hourly, daily, weekly, monthly
2LOGFILE='imageOptimization.log'
3IMGDIR='' # no trailing forward slash please
4
5# INSTALL TOOLS
6# install Jpegoptim and OptiPNG
7sudo apt update
8sudo apt install jpegoptim optipng
9
10# OPTIMIZATION
11find ${IMGDIR}/ *.png -exec optipng -silent -preserve {} \;
12find ${IMGDIR}/ *.{jpeg,jpg} -exec jpegoptim {} \;
13
14# CRON
15
16# LOGGING
17# find out how long the command took, add that to the log
18# how many files were compressed, details of task etc.
19# send log via email (or look into some sort of mobile notifications e.g. https://simplepush.io/)
20
21# TODO
22# [ ] setup cron
23# [ ] setup logging
24# [ ] accept multiple dirs and loop over them
Optipng options:
-preserve
Preserve file attributes (time stamps, file access rights, etc.) where applicable.
-quiet, -silent
Run in quiet mode.
The messages are still written to the log file if the option -log is enabled.