Get base product image in Magento

64,293

Solution 1

I think you are looking for this:

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

Credit should be given to BenMarks who gave this answer.

Solution 2

Try:

$this->helper('catalog/image')->init($_product, 'image')->keepFrame(false)
->constrainOnly(true)->resize(38,38);

Solution 3

BE AWARE!

$this->helper('catalog/image')->init($_product, 'small_image')->resize(38, 38);

is object, not url string it self. Yes, you can use it directly with echo, but shouldn't assign it to var. For example this wont work:

    $images = array();
    foreach($products as $_product){
        $images[]=$this->helper('catalog/image')->init($_product, 'small_image')
        ->resize(38, 38);
    }

After foreach, you will have only one last image url saved. Simple way is to get truly string url is:

$images = array();
foreach($products as $_product){
    $images_obj = $this->helper('catalog/image')->init($_product, 'small_image')
    ->resize(38, 38);
    $images[] = (string)$images_obj;
}

Solution 4

The reason this is happening is because the image attribute is not loaded in the product listing. Normally you can change this while editing the attribute, but you can't edit those settings for this attribute. I think that's because it is a stock attribute.

TLDR;

UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106;

** Warning, you should not execute this ^^^ query until you are certain your image attribute_id for the catalog_product entity is 106!

Some answers are suggesting this method:

$_product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());

echo $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38);

You should not do this! This is because you will do a full product load! This is not efficient, and is most likely being done inside of a loop which is even worse! Sorry for screaming!

I usually do not condone direct DB edits, but in this case it was the easiest solution for me:

# First make sure we are using all the right IDs, who knows, I have seen some fubar'ed deployments

SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product';
# 10
SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'image' AND entity_type_id = 10;
# 106

# Now that we know the exact attribute_id....
UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106;

Now the image attribute data will be automagically loaded on the product listing pages, then you can access it like this:

echo $this->helper('catalog/image')->init($_product, 'image');

The best part is that you are not loading the entire product in a loop! DO NOT EVER DO THAT EVER

** Also, because I know I am going to get people saying that this is not the Magento way, the alternative would be to create a module that has an SQL setup script that runs the command.

Solution 5

Small image and thumbnail works great.

Then try small_image instead of image, like this:

echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(38, 38);
Share:
64,293
Dave
Author by

Dave

Updated on April 15, 2020

Comments

  • Dave
    Dave about 4 years

    I want to get base product image in Magento to resize it and display in cart sidebar.

    Unfortunatelly this:

    echo $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38);
    

    prints Magento placeholder image.

    Base image is set for this product properly. Small image and thumbnail works great.

    No idea what's going on.

    EDIT: Solution: Get full product data this way:

    $_product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());
    

    and then use it as you wish:

    echo $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38);