5/04/2011

How to get all gallery image with resize of a product in magento

In product page you can get gallery image of the current product, But if you think to fetch all gallery image of a product in other page like cms page, then you have to write the code with little bit modification. The code should looks like below

<?php
$obj = new Mage_Catalog_Block_Product_View_Media();
$_product = new Mage_Catalog_Model_Product();
// Load all product information of a particular product
$Products_one = Mage::getModel('catalog/product')->load($id);
// Use your Product Id instead of $id
$countt = count($Products_one->getMediaGalleryImages());
if($countt>0){
foreach ($Products_one->getMediaGalleryImages() as $_image)
{
// For the Original Image
echo "<img src=".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).str_replace(Mage::getBaseUrl('media'),"",$_image->url)." alt='' />";
//For gallery Image
$resizeimage = $obj->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->backgroundColor(242,242,243)->resize(400,300);
echo "<img src=".$resizeimage."alt='' />";
}
}

1 comment:

  1. Thanks, this works fine in Magento 1.7.0.2
    You saved lot of my time :-)

    ReplyDelete