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


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) ?>"/>

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
    What is $image here? I am new to Magento Extension Development. – Sumit Kumar Sep 24 '17 at 7:00

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());

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) ?>"/>
  • Which one will be great considering performance?? – Kaushal Suthar Oct 5 '16 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. – Ashish Jagnani Oct 5 '16 at 10:42
  • Another thing I forgot to mention is I want to resize image as well – Kaushal Suthar Oct 5 '16 at 10:43

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.