Magento Image Optimisation
Aug 18, 2017 · 2 minute readCategory: magento
The Problem
One of our clients recently needed a simple solution for automatically optimising their
media/
and skin/
images. After trying a reputable module we decided it would be
simpler and give us more control if we completed this using bash
, cron
, jpegoptim
and optipng
.
The Solution
Usage
- Set the path to your
media/
andskin/
directories. This is relative to the scripts location. - Install
jpegoptim
andoptipng
. These will likely be available in your package manager if you use Linux. - Backup your current
media/
andskin/
directories. - Set arguments for
jpegoptim
andoptipng
in theoptimiseJpeg()
andoptimisePng()
functions respectively. In the end we were able to get the results we needed using only lossless compression. This is what the script is currently configured for. - Make a first pass through all the images so that existing images are compressed. This
can be done by running the script with a large period. I used
5256000
for a period of 10 years.
bash img_optimiser.bash 5256000 &>> img_optimiser.log
- Setup
cron
to run the script regularly and optimise any new images as they’re added. You’ll need to set the period (in minutes) to match the period of yourcron
job. I’m running the script every hour with a period of 60.
0 * * * * bash /path/to/img_optimiser.bash 60 2>&1 >> /path/to/img_optimiser.log
The 2>&1
allows you to log stdout
and stderr
into one file from cron.