Get original image file from a cache URL

The image URL that you've given is impossible to have with a Magento store. I'm guessing you wanted it to be obfuscated, but in doing so have made it impossible to answer.

I can't think of any reasons to de-construct the image path unless you are web-scraping another website, otherwise, you would have direct access to the product model and could fetch this information directly.

Nevertheless, using a real example. Eg

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

De constructing 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

Attributions: sonassi.com


Short answer - no. There is no method to return the base image based on the cache image url. However, if you remove the cache folder and the hash you should be able to intuit what the URL would be, e.g.:

 http://www.mysite.com/media/catalog/product/cache/1/image/109x298/9df78eab33525d08d6e5fb8d27136e95/u/t/uti001_2.jpg

remove everything after product until the prefix trie (in this case, remove cache/1/image/109x298/9df78eab33525d08d6e5fb8d27136e95/:

 http://www.mysite.com/media/catalog/product/u/t/uti001_2.jpg

Perhaps a better way of dealing with this, though, is to use the object model:

$product = Mage::getModel('catalog/product')->load(42);
echo $product->getImage();
echo $product->getThumbnail();

This works

   $r = explode('/' ,$imagesCachePath);

   unset($r[6]);
   unset($r[7]);
   unset($r[8]);
   unset($r[9]);

   $r = implode('/',$r);