From the code below I get the image of a product from a url:

$media = Mage::getModel('catalog/product_media_config')->getMediaUrl($product->getThumbnail());

And with that it returns me the image from the URL by picking directly from the product:


But for my need, in this way, even resizing the image, it gets distorted and I needed to get the image through the cache and in case it would have to return the image from the URL below, however I do not know what I should change to do that:


If you are getting product data e.g

$_product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());

Then the following should work perfectly fine

$media - $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38);

Tested and working on 1.9 this will return the URL so you can use like

<img src="<?php echo $media; ?>" />
| improve this answer | |
  • Returned error 404. – Matheus Portela Sep 5 '17 at 20:47
  • But I wanted to assign this path in the $media variable, how should I do it? – Matheus Portela Sep 6 '17 at 16:22
  • have now updated my answer – Dava Gordon Sep 6 '17 at 19:33

If i understood well you want to resize your image then you get it from a cache, so you have to do this:

in list.phtml for exemple:

app/design/frontend/{package}/{currentTheme}/template/catalog/product.list.phtml, you update the current one with :

<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(50, 50); ?>

then you will get it like your exemple :


/media/catalog/product/cache/ = cache directory.

1/ = store ID.

image/ = subdirectory. // image/ or small_image/ or thumbnail/

50x50/ = image size.

9df78eab338d27136e95/= image cache hash.

i/ = first letter of image name.

m/ = second letter of image name.

image-name.jpg= image name.

| improve this answer | |
  • This code returns me error 500, as I'm assigning the path to the $media variable. – Matheus Portela Sep 6 '17 at 16:51
  • Hein ? Dont assign anything, like i explained in my answer with the fonction, magento resize automatically the image and stock it in media, you hanevn't to set path... – PЯINCƏ Sep 6 '17 at 17:03
  • Yes, but I need to put this value in the $media variable. – Matheus Portela Sep 6 '17 at 17:08

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.