Magento – get product image url from getItemsCollection

collection;imagemagento-1.7

I'am trying to get the image product url from getItemsCollection generated by Mage_Reports_Block_Product_Viewed. Here is my code. Everything is working except getImageUrl()

$nb = 3;
$already_used = array();
$collection = Mage::getSingleton('Mage_Reports_Block_Product_Viewed')->getItemsCollection();
foreach ($collection as $item){
    if ($nb > 0)
    {
        ?>
        <div class="product">
            <div class="image_product">
                <a href="<?php echo $item->getProductUrl(); ?>"><img src="<?php echo $item->getImageUrl(); ?>" alt="<?php echo $item->getName();?>" /></a>
            </div>
            <div class="title_product">
                <h3><a href="<?php echo $item->getProductUrl(); ?>"><?php echo $item->getName(); ?></a></h3>
            </div>
        </div>
        <?php
        $already_used[] = (int)$item->getProductId();
    }
    $nb--;
}

I don't understand what is wrong in my code…

Thank you for your help !

Best Answer

$item should be an instance of Mage_Catalog_Model_Product so you should be able to get the image url using the catalog image helper:

$imageUrl = (string)Mage::helper('catalog/image')->init($item, 'image');

[EDIT]
So your code should look like this (only the section that should display the image)

<div class="image_product">
    <a href="<?php echo $item->getProductUrl(); ?>">
       <img src="<?php echo Mage::helper('catalog/image')->init($item, 'image')/*->resize(200, 200)*/; ?>" alt="<?php echo $item->getName();?>" />
    </a>
</div>

If you want the image at a specific size, just uncomment the resize method and change the width and height.