1

I want to change the design product image section on view page.

Trying to fetch all three Images seprately.

 $ImageAll = $_product->getMediaGalleryImages(); 
        $mainImg = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(163, 10);
        $thumbNailImg = Mage::helper('catalog/image')->init($_product, 'thumbnail')->resize(400, 400);
        $smallImg = Mage::helper('catalog/image')->init($_product, 'image')->resize(60,60);

One more thing if we change the position of above variable then the last variable is only used

But it gives me the same image in all the three images tags.

<img src="<?php echo $mainImg; ?>" alt="<?php echo $_product->getName()?>" />
<img src="<?php echo $thumbNailImg; ?>" alt="<?php echo $_product->getName()?>" />
<img src="<?php echo $smallImg; ?>" alt="<?php echo $_product->getName()?>" /> 

all imgage

BUt if i use this way all works perfectly

<img src="<?php echo Mage::helper('catalog/image')->init($_product, 'image')->resize(100,160); ?>" alt="<?php echo $_product->getName()?>" />

 <img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(200, 200); ?>" alt="<?php echo $_product->getName()?>" />
 <img src="<?php echo Mage::helper('catalog/image')->init($_product, 'thumbnail')->resize(400, 400); ?>" alt="<?php echo $_product->getName()?>" />

Can anyone help me to understand the difference between them?

  • can you confirm that both given code working differently on same product's page or product was diferrent? – Piyush Oct 5 '17 at 5:37
  • on same product page – inrsaurabh Oct 5 '17 at 5:41
  • I tried it in my sysetm , it is same there, STRANGE – Piyush Oct 5 '17 at 5:58
  • Yes ,i have no idea how magneto reads the page, – inrsaurabh Oct 5 '17 at 6:04
2

Mage::helper('catalog/image') is a class. Think of it as a collection in Magento.

When you write

$mainImg = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(163, 10);
$thumbNailImg = Mage::helper('catalog/image')->init($_product, 'thumbnail')->resize(400, 400);
$smallImg = Mage::helper('catalog/image')->init($_product, 'image')->resize(60,60);

is like writing

    $mainImg = $foo;
    $thumbNailImg = $foo;
    $smallImg = $foo;

    $foo = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(163, 10)->init($_product, 'thumbnail')->resize(400, 400)->init($_product, 'image')->resize(60,60);

If you to define all three variables before echoing them you have to clone the helper.

$mainImg = clone Mage::helper('catalog/image')->init($_product, 'small_image')->resize(163, 10);
$thumbNailImg = clone Mage::helper('catalog/image')->init($_product, 'thumbnail')->resize(400, 400);
$smallImg = clone Mage::helper('catalog/image')->init($_product, 'image')->resize(60,60);
0

When a Image is uploaded to product Gallery, Magento Creates Three copy of that image :

  1. Main Image - Shown in Magnifier when Click on Product Image on Detail Page

  2. Small Image - Shown in Product List and Product Detail Page

  3. Thumbnails - Shown in When you change from Grid to List in Product List Page

So you are fetching all three copies of single image.

You should have to do :

    foreach ($_product->getMediaGalleryImages() as $image) 
    {
        echo $image->getUrl();
    }

    Or

    <?php foreach ($_product->getMediaGalleryImages() as $image) :?>

        <img src="<?php echo Mage::helper('catalog/image')->init($_product, 'image', $image->getFile())->resize(100, 100); ?>" alt="<?php echo $_product->getName()?>" />

    <?php endforeach; ?>  
  • i know about the loop, i want to know why it behaves like this, if i use them seprately, – inrsaurabh Oct 5 '17 at 5:56
  • I think if you check "init" function of Mage::helper('catalog/image') you will see it calls view.xml file of your theme. If you check view.xml, you can see that the default size for product images are set there for product detail page images. That is what conflicts with the sizes you are setting by resize() function. @SaurabhRanjan . – Sujeet Pandit Oct 5 '17 at 6:51

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.