Home > Magento Tips > Using the Image Helper

Using the Image Helper

Posted on: 22nd Feb 2011 By: Robert Kent 2 Comments

Just a quick one today about using the image helper in magento.

With just one little snippit of code:

$_imagehelper = Mage::helper('catalog/image');

You can bring back your product images with ease!

There’s been many times in the past when we have used code etc to load product collections outside of the catalog – for instance – the best seller static blocks or dynamic sitemaps. In these instances it is important to use the image helper to return your product image within the system – utilizing the cache and resize options. It is this product object that we need in order to feed it into the helper and bring back our correct image.

If you only have a product ID or SKU to work with then you will need to load the product with either of the following methods before continuing.

$productid = Mage::getModel('catalog/product')->getIdBySku($sku); // if only got SKU
$product = Mage::getModel('catalog/product')->load($productid);

Now we have our product object we can load it into our image helper like so:

<img src="<?php echo $_imagehelper->init($product, 'image')->resize(100) ?>" alt="My product image" />

This should bring our product image back! As you can see its helper->load our product->get our image->resize our image -> echo the result.

I hope this has been of some assistance and thank you for visiting our Magento Blog – Here at Ecommerce Web Design!

2 Responses to “ Using the Image Helper ”

  1. haltabush
    #1 | 17th March 2011

    Thanks, works great :)
    By the way, any idea on how we can know if the returned image is a placeholder (i.e. product image doesn’t exists)?

  2. Redas
    #2 | 30th December 2011

    Thanks for this small article

    Here’s one line code if we already have product ID

    Mage::helper('catalog/image')->init(Mage::getModel('catalog/product')->load($id), 'small_image')->resize(32);
    

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.