How to get a product's image in Magento?
echo $_product->getImageUrl();
This method of the Product class should do the trick for you.
You can try to replace $this->
by Mage::
in some cases. You need to convert to string.
In my case i'm using DirectResize extension (direct link), so my code is like this:
(string)Mage::helper('catalog/image')->init($_product, 'image')->directResize(150,150,3)
The ratio options (3rd param) are :
- none proportional. The image will be resized at the Width and Height values.
- proportional, based on the Width value 2
- proportional, based on the Height value 3
- proportional for the new image can fit in the Width and the Height values. 4
- proportional. The new image will cover an area with the Width and the Height values.
Update: other info and versions here
The common way, without plugin would be:
(string)Mage::helper('catalog/image')->init($_product, 'image')->resize(150)
You can replace 'image' with 'small_image' or 'thumbnail'.
I recently needed to do this as well... here's how I got to it:
$_product->getMediaGalleryImages()->getItemByColumnValue('label', 'LABEL_NAME')->getUrl();
Hope that helps you!
Here is the way I've found to load all image data for all products in a collection. I am not sure at the moment why its needed to switch from Mage::getModel to Mage::helper and reload the product, but it must be done. I've reverse engineered this code from the magento image soap api, so I'm pretty sure its correct.
I have it set to load products with a vendor code equal to '39' but you could change that to any attribute, or just load all the products, or load whatever collection you want (including the collections in the phtml files showing products currently on the screen!)
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addFieldToFilter(array(
array('attribute'=>'vendor_code','eq'=>'39'),
));
$collection->addAttributeToSelect('*');
foreach ($collection as $product) {
$prod = Mage::helper('catalog/product')->getProduct($product->getId(), null, null);
$attributes = $prod->getTypeInstance(true)->getSetAttributes($prod);
$galleryData = $prod->getData('media_gallery');
foreach ($galleryData['images'] as &$image) {
var_dump($image);
}
}
First You need to verify the base, small and thumbnail image are selected in Magento admin.
admin->catalog->manage product->product->image
Then select your image roles(base,small,thumbnail)
Then you call the image using
echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(163, 100);
Hope this helps you.