Get base product image in Magento
Asked Answered
J

7

16

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);
Jacquesjacquet answered 16/8, 2012 at 20:48 Comment(1)
Welcome to Stack Overflow. It's absolutely OK to self-answer your own question, but please post it as actual answer rather than inside the question itself. This allows the answer to be voted/accepted and helps us to keep the "Unanswered" list more clear.Foreland
A
21

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.

Adria answered 16/8, 2012 at 20:54 Comment(4)
$product->getImage() returns null. With small and thumbnail works. Does it matters its called in Mage_Checkout_Block_Cart_Sidebar?Jacquesjacquet
if you have $item as product in cart try first: $product=Mage::getModel('category/product')->load($item->getId()); echo Mage::getModel('catalog/product_media_config') ->getMediaUrl( $product->getImage() );Albescent
Jerzy, almost :) $_product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId()); And finally it's working as intended. Dzięki.Jacquesjacquet
This is good if all you have is the image URL. but as far as I know you can not resize this to any size you want :(Criss
I
3

Try:

$this->helper('catalog/image')->init($_product, 'image')->keepFrame(false)
->constrainOnly(true)->resize(38,38);
Idiomatic answered 16/8, 2012 at 20:54 Comment(0)
H
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;
}
Higinbotham answered 10/10, 2013 at 8:24 Comment(2)
thanks a lot! Helped me out a ton. Instead of adding . '' I converted it to a (string). Not sure if its the best practice.Lockage
I guess more obvious conversion to a string is might be a bit more clear - $images[] = $images_obj.'' >>> $images[] = (string)$images_obj;Snowslide
M
2

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.

Mccauley answered 14/4, 2017 at 22:59 Comment(0)
O
1

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);
Oliy answered 16/8, 2012 at 20:59 Comment(2)
But I want to get base image, not small_image. It matters for me.Jacquesjacquet
@marco see my answer belowMccauley
A
0
<img src='.$this->helper('catalog/image')->init($product, 'small_image')->resize(225, 225).' width=\'225\' height=\'225\'/>
Attenuator answered 8/4, 2014 at 13:43 Comment(0)
L
0

Magento Product image assigning to Variable

$product_image = Mage::helper('catalog/image')->init($productmodel,'small_image')->keepFrame(true)->resize(width,height).'';

Magento Product image assigning to Object

$products[] = Mage::helper('catalog/image')->init($productmodel,'small_image')->keepFrame(true)->resize(width,height).'';

It Works for me....

Ligan answered 25/9, 2014 at 7:47 Comment(2)
Appending it to a string is somewhat ambiguous, it would be nicer to typecast it instead. $product_image = (string) Mage::helper('catalog/image')->init($productmodel,'small_image')->keepFrame(true)->resize(width,height);Mickey
@Mickey I have added .'' at end check my answer now.Ligan

© 2022 - 2024 — McMap. All rights reserved.