2

I want to show all product images in product listing page(list.phtml file).

So to do so I've done following in list.phtml

var_dump($_product->getMediaGalleryImages());

But it returns with NULL.

Another way I found is as following

$mediaApi = Mage::getModel('catalog/product_attribute_media_api');
$mediaItems = $mediaApi->items($_product->getId());
foreach ($mediaItems as $image) {
    echo $image['file'] ."<br>";
}

But it return me half path like /p/a/pawo.jpg.

So is there any better way to get all product images(Gallery) in product listing page??

NOTE : I also need to resize image like we do as following.

 <img id="product-image-<?php echo $_product->getId(); ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(TRUE)->keepAspectRatio(FALSE)->keepFrame(FALSE)->resize($_imgSize,$_imgSize); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"/>

2 Answers 2

2

You can try below code. You canget path using catalog helper

echo Mage::helper('catalog/image')->init($_product, 'image', $image['file']) ."<br>";

From this code you can get cache path also.

1
  • 1
    What is $image here? I am new to Magento Extension Development.
    – Sumit
    Sep 24, 2017 at 7:00
2

In the second method you are getting proper path, just you have to add media path of catalog product like ::

$mediaPath = Mage::getBaseUrl('media').'catalog/product'

The proper solution is :

$mediaApi = Mage::getModel('catalog/product_attribute_media_api');
$mediaItems = $mediaApi->items($_product->getId());
foreach ($mediaItems as $image) {
   echo $mediaPath.$image['file'] ."<br>";
}

In first method you have to load product first like :

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

To resize use :

<img id="product-image-<?php echo $_product->getId(); ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image',$image['file'])->constrainOnly(TRUE)->keepAspectRatio(FALSE)->keepFrame(FALSE)->resize($_imgSize,$_imgSize); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"/>
3
  • Which one will be great considering performance?? Oct 5, 2016 at 10:40
  • I think product load in list page will take load time so its better to use second solution. It will load only product media not all attribute of product. Oct 5, 2016 at 10:42
  • Another thing I forgot to mention is I want to resize image as well Oct 5, 2016 at 10:43

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.