33

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
73

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

  • 3
    Of course you have an incredibly exhaustive answer with ascii art... :) – philwinkle Feb 26 '13 at 16:44
  • 6
    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. – Ben Lessani - Sonassi Feb 26 '13 at 16:47
  • 1
    sonassi, I have no idea, where your time comes from, but you are a big win for the community and us all! – Fabian Blechschmidt Feb 26 '13 at 17:44
  • That was very useful to me, and one of the best answers I ever read. Thanks! – LeCodeur Jun 9 '17 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);
  • it is not probably the best approach but works fine. why downvote ? – Fra Aug 10 '13 at 8:58
  • Won't this produce inconsistent results if there are image dimensions in the filename, since this would offset the indexes? – Eric Seastrand Aug 8 '17 at 13:10
4

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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