0

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:

http://urlsite.com/media/catalog/product/u/m/uma-flor-alaranjada-com-mensagem-cart_o-14896928_1_1.jpg

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:

http://urlsite.com/media/catalog/product/cache/1/thumbnail/50x50/9df78eab33525d08d6e5fb8d27136e95/u/m/uma-flor-alaranjada-com-mensagem-cart_o-14896928_1_1.jpg

2 Answers 2

1

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; ?>" />
3
  • Returned error 404. Sep 5, 2017 at 20:47
  • But I wanted to assign this path in the $media variable, how should I do it? Sep 6, 2017 at 16:22
  • have now updated my answer Sep 6, 2017 at 19:33
1

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/1/image/50x50/9df78eab338d27136e95/i/m/image-name.jpg

/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.

3
  • This code returns me error 500, as I'm assigning the path to the $media variable. Sep 6, 2017 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, 2017 at 17:03
  • Yes, but I need to put this value in the $media variable. Sep 6, 2017 at 17:08

Your Answer

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

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