Shrink your Magento media folder (for development)

Reduce the size of your production media data to a manageable size for you development environments
created by on 2015-11-08

This should be obvious but I’ll say it anyway. Don’t perform these steps on your live system. The quality of your images will be reduced, images might get lost.

1. Remove temporary files

The first step should be removing temporary data from your media folder:

cd <magento-root>

rm -rf media/css/* media/css_secure/* media/captcha/* media/reports_export/*
find media -type d -name "cache" -o -name "tmp" -o -name "import" | xargs rm -rf

2. Remove unused images

This step is not an easy one because images can be referenced in many different ways. But Peter Jaap Blaakmeer has created a script which does the job pretty well: peterjaap/cleanImages.php

Besides other things the script uses the tool fdupes to find duplicate images. Please install it before using the script.

apt-get install fdupes

Download the script to your shell-folder:

cd <magento-root>
curl https://gist.githubusercontent.com/peterjaap/5547654/raw/6e9b8def5fdbc30846a39be3c00b3e34cec67784/cleanImages.php > shell/cleanImages.php

Set the options for the script:

$cleanUpDuplicates = true;
$countProductWithoutImages = true;
$cleanUpOrphans = true;
$cleanUpTableRowsMediaGallery = true;
$cleanUpTableRowsVarchar = true;
$setDefaultImageForProductsWithoutDefaultImage = true;

Execute the script:

cd <magento-root>
php shell/cleanImages.php

3. Reduce Image Size

JPEG

https://github.com/tjko/jpegoptim

cd <magento-root>

find media -iname "*.jpg" -exec jpegoptim -m20 --strip-all -o -t {} \;

PNG

The size of PNGs can be reduced with pngcrush.

This bash script finds all PNGs in your current folder and uses pngcrush -reduce to strip some unnecessary data from your PNGs:

cd <magento-root>

find media -iname "*.png" | while read png;
do
  echo "crushing $png"
  pngcrush -reduce "$png" /tmp/temp.png
  mv -f /tmp/temp.png "$png"
done;

But note, reducing the size of PNGs takes a lot of time and does not help all that much. The biggest gain can be achieved by reducing the size of JPEGs.

4. Create an archive

cd <magento-root>
currentdate=$(date +"%y-%m-%d")

tar -cvf "media-$currentdate.tar" -C media .

Or you can create the media backup with magerun.

cd <magento-root>
magerun media:dump
Tags: