35

Is there any function that given a cache image product file return the original one?

Example :

=> Cache URL:

{BASEURL}/media/catalog/product/cache/2/image/9df78eab33525d08d6e5fb8d27136e95/a/l/IMAGE.jpg

=> Desired outcome:

BASEURL/media/catalog/product/a/l/IMAGE.jpg

5 Answers 5

75

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

4
  • 3
    Of course you have an incredibly exhaustive answer with ascii art... :)
    – philwinkle
    Commented Feb 26, 2013 at 16:44
  • 7
    LOL - I was waiting for someone to comment on the ASCII art :) Makes it worthwhile that it took 10 minutes to draw/type! Clearly we're unofficially posting the Magento-1 documentation answer by answer. Commented Feb 26, 2013 at 16:47
  • 2
    sonassi, I have no idea, where your time comes from, but you are a big win for the community and us all! Commented Feb 26, 2013 at 17:44
  • That was very useful to me, and one of the best answers I ever read. Thanks!
    – LeCodeur
    Commented Jun 9, 2017 at 10:46
8

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();
5

This works

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

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

   $r = implode('/',$r);
2
  • it is not probably the best approach but works fine. why downvote ?
    – Fra
    Commented Aug 10, 2013 at 8:58
  • Won't this produce inconsistent results if there are image dimensions in the filename, since this would offset the indexes? Commented Aug 8, 2017 at 13:10
4

In the list.phtml, this is what worked for me:

echo Mage::getModel('catalog/product_media_config')->getMediaUrl( $_product->getSmallImage()); 
0

If you want to get the original image path without cache part you can use this method: \Magento\Catalog\Model\Product\Media\Config::getMediaUrl()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.