Magento: How to get thumbnail?

13,760

Solution 1

I recently needed to do this as well... here's how I got to it:

$_product->getMediaGalleryImages()->getItemByColumnValue('label', 'LABEL_NAME')->getUrl();

Or another example

You should use the catalog product media config model for this purpose.

<?php

//your code ...

echo Mage::getModel('catalog/product_media_config')
            ->getMediaUrl( $product->getImage() ); //getSmallImage(), getThumbnail()

Edit After your comment.

Update Answer

See below URL

Make all store images the base, small and thumbnail images in Magento?

Try it

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach ($products as $product) {
    if (!$product->hasImage()) continue;
    if (!$product->hasSmallImage()) $product->setSmallImage($product->getImage());
    if (!$product->hasThumbnail()) $product->setThumbnail($product->getImage());
    $product->save();
}

Hope that helps you!

Solution 2

If you want to see which data you can access you could try this:

<?php var_dump(array_keys($product->getData())); ?>

You can then use two methods to get this data:

<?php $product->getAttributeName() // Use CamcelCase ?>

or

<?php $product->getData('attribute_name') // underscores ?>
Share:
13,760
Linkjuice57
Author by

Linkjuice57

Updated on June 04, 2022

Comments

  • Linkjuice57
    Linkjuice57 almost 2 years

    I downloaded a script to create a CSV datafeed of my products and I would also like to include a url to the thumbnail. The code already has the following for the product image url:

    $product_data['ImageURL']=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();

    So I tried to adjust this to:

    $product_data['ThumbnailURL']=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getThumbnail();

    Which displays exactly the same image url (not the thumb). How would I fix this?

    I did a var_dump($product); and the result was:

    ["image"]=> string(18) "/f/i/file_2_18.png" ["small_image"]=> string(18) "/f/i/file_2_18.png" ["thumbnail"]=> string(18) "/f/i/file_2_18.png"

    I also need to get the subcategory for the product but I don't know how to call this. How can I see which variables are possible? E.g. $product->getPrice or $product->getName?

  • benmarks
    benmarks over 11 years
    Wow, what a great answer... :-)
  • benmarks
    benmarks over 11 years
    Varien_Object::debug() offers a bit more utility and insight.
  • Linkjuice57
    Linkjuice57 over 11 years
    Problem is that I get the exact same image when I echo Image Thumbnail or SmallImage. Is this then just a problem with my store?
  • Abid Hussain
    Abid Hussain over 11 years
    See below URL it is help full. stackoverflow.com/questions/2474117/…
  • Abid Hussain
    Abid Hussain over 11 years
    I have update my answer after you above comment. please take a look. And see also this url stackoverflow.com/questions/4349592/…