Deconstructing the cache image path on Magento

Deconstructing the cache image path

/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/m/e/me-d1_2.jpg
|___________________________|_|_____|________________________________|_|_|__________|
             |               |   |                  |                 | |     |
       Cache Directory       |   |                  |                 | |     |
Mage/Catalog/Model/Product/Media/Config.php         |                 | |     |
getBaseMediaPath()           |   |                  |                 | |     |
                             |   |                  |                 | |     |
                             |   |                  |                 | |     |
                          Store ID                  |                 | |     |
                          Mage::app()->getStore()->getId()            | |     |
                                 |                  |                 | |     |
                                 |                  |                 | |     |
                            Subdirectory            |                 | |     |
                            Mage/Catalog/Model/Product/Image.php      | |     |
                            getDestinationSubdir()  |                 | |     |
                                                    |                 | |     |
                                                    |                 | |     |
                                              MD5 of Image Params     | |     |
                                              See below               | |     |
                                              Mage/Catalog/Model/Product/Image.php
                                              Line +324               | |     |
                                                                      | |     |
                                                                      | |     |
                                                          First letter of Image Name
                                                                        |     |
                                                                        |     |
                                                          Second letter of Image Name
                                                                              |
                                                                              |
                                                                     Original Image Name

Locating the original image

If uploaded via the Magento admin, the original image is located at /media/catalog/product/m/e/me-d1_2.jpg

So getting the original URL is pretty easy, just remove the cache directory path and hash.

Ie. Remove cache/1/image/9df78eab33525d08d6e5fb8d27136e95/

So

/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/m/e/me-d1_2.jpg

Becomes

/media/catalog/product/m/e/me-d1_2.jpg

If uploaded via dataflow/import, the original image is located in /media/import

But beyond there would be guesswork. However people want to format their upload DIR for import is really up to them and the subdirectory the images are in really isn't relevant - as they provide the relative path to the image when uploading.

A bit about the md5 Hash

($this->_keepAspectRatio  ? '' : 'non') . 'proportional',
($this->_keepFrame        ? '' : 'no')  . 'frame',
($this->_keepTransparency ? '' : 'no')  . 'transparency',
($this->_constrainOnly ? 'do' : 'not')  . 'constrainonly',
$this->_rgbToString($this->_backgroundColor),
'angle' . $this->_angle,
'quality' . $this->_quality,

Optional args if there is a watermark

$this->getWatermarkFile(),
$this->getWatermarkImageOpacity(),
$this->getWatermarkPosition(),
$this->getWatermarkWidth(),
$this->getWatermarkHeigth()

md5(implode('_', $miscParams));

So for example, with the defaults

md5('non_no_no_not_ffffff_0_90')

The md5 hash could be the same across the entire site - for every image, but you would need to figure out the values used to generate it. You could brute force it relatively easily.

This is the default Magento value for the hash

9df78eab33525d08d6e5fb8d27136e95