How many ways to get an image in Magento!

Anyway, I'm trying to get an image. I'm in a foreach loop like this:

<?php foreach ($this->getItems() as $key=>$item): ?> 

Product details are getting displayed like this:

<?php echo $this->escapeHtml($item->getProductSku()) ?>

But I can't seem to get the image to show and I've many ways.

How can I get the image to show?

  • Where and what image you want to display in foreach loop?Can you give me more details about this – Ajithkumar Dec 12 '18 at 15:04
  • At this point any image will do : ) But preferably the thumbnail. It's in the customer account page. Does that help? – Freejoy Dec 12 '18 at 15:24

Product images are typically rendered using the Mage_Catalog_Helper_Image class in the following way:

  1. Create an instance of the helper image class: $imageHelper = Mage::helper('catalog/image');
  2. Provide the instance with the product and image attribute code that you want to render: $imageHelper->init($product, $imageAttributeCode);
  3. Specify width and height to which the image should be resized: $imageHelper->resize($width, $height);
  4. Call $imageHelper->__toString() to generate the image in media/catalog/product/cache (it it doesn't already exist) and return an url to it. When calling this method in a phtml, you can just use <?php echo $imageHelper ?>, which automatically triggers the __toString method.

In most cases, the above steps are executed inline:

<?php echo Mage::helper('catalog/image')->init($product, $imageAttributeCode)->resize($width, $height) ?>

So for the case of rendering a 75x75 thumbnail of the products shown on the sales order view page, you can add the following to a template override of app/design/frontend/base/default/template/sales/order/items/renderer/default.phtml:

    src="<?php echo Mage::helper('catalog/image')->init($_item->getProduct(), 'thumbnail')->resize(75, 75) ?>"
    alt="<?php echo $this->escapeHtml($_item->getName()) ?>"

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.