1

Magento Image Optimisation

 2 years ago
source link: https://edmondscommerce.github.io/magento-image-optimisation/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Magento Image Optimisation

Aug 18, 2017 · 2 minute read
Category: 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

#!/usr/bin/env bash readonly DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; cd $DIR; set -e set -u set -o pipefail standardIFS="$IFS" IFS=$'\n\t'

# To use this script you'll need to install: # - jpegoptim # - optipng # # As well as set the mediaDir and skinDir below.

function echoTopLevelMsg() { echo "[ $(date +%Y.%m.%dT%H:%M:%S) ] ===== $1 ====="; }

echoTopLevelMsg "Running optimisation script";

function usage() { echo " $0 <period> - period: number of minutes since the script last ran (defaults to all images) "; }

function checkDependency() { local dependency=$1;

command -v ${dependency} >/dev/null 2>&1 || { echo >&2 "Please install '${dependency}' and then rerun this script."; exit 1; } }

function processImages() { local baseDir="$1"; local match="$2"; local tool="$3"; shift 3;

echo "Processing images using: baseDir=${baseDir} period=-${period} match=${match} tool=${tool} toolArgs=$@";

# Used \; in place of + because jpegoptim isn't able to handle large numbers of files in one go. local periodFilter="" if [[ "${period}" != "0" ]] then find ${baseDir} -type f -mmin -${period} -iregex ${match} -exec ${tool} "$@" {} \; else find ${baseDir} -type f -iregex ${match} -exec ${tool} "$@" {} \; fi

echo "Processing complete"; }

function optimiseJpeg() { local match='.*\.jpg\|.*\.jpeg'; local tool='jpegoptim';

checkDependency ${tool};

processImages ${mediaDir} ${match} ${tool} --strip-all -o; processImages ${skinDir} ${match} ${tool} --strip-all -o; }

function optimisePng() { local match='.*\.png'; local tool='optipng';

checkDependency ${tool};

processImages ${mediaDir} ${match} ${tool} -o2 -strip all; processImages ${skinDir} ${match} ${tool} -o2 -strip all; }

#defaults to processing all readonly period=${1:-0};

readonly mediaDir="${DIR}/path/to/media"; readonly skinDir="${DIR}/path/to/skin";

if [ ! -d "${mediaDir}" ]; then echo "${mediaDir} does not exist"; exit 1; fi

if [ ! -d "${skinDir}" ]; then echo "${skinDir} does not exist"; exit 1; fi

echoTopLevelMsg "Optimising JPG's"; optimiseJpeg;

echoTopLevelMsg "Optimising PNG's"; optimisePng;

Usage

  • Set the path to your media/ and skin/ directories. This is relative to the scripts location.
  • Install jpegoptim and optipng. These will likely be available in your package manager if you use Linux.
  • Backup your current media/ and skin/ directories.
  • Set arguments for jpegoptim and optipng in the optimiseJpeg() and optimisePng() 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 your cron 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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK