I have a helper from which to add a product through the id. After the product is added, I save some product data (name, price, and image) into an array and return it. But I'm getting the image directly from the product, but I need to get that same image from the magento cache in my specific case.

Helper Code:

public function addCustom($product_id)
    // Get customer session
    $session = Mage::getSingleton('customer/session'); 
    // Get cart instance
    $cart = Mage::getSingleton('checkout/cart'); 
    // Add a product with custom options
    $product = Mage::getModel('catalog/product')->load($product_id);

    $param = array(
        'product' => $product->getId(),
        'qty' => 1
    $request = new Varien_Object();
    $cart->addProduct($product, $param);            
    // update session
    // save the cart

    $nome = $product->getName();
    $preco = number_format((float)$product->getPrice(), 2, ',', '');
    $media = Mage::getModel('catalog/product_media_config')->getMediaUrl($product->getThumbnail());
    $img = $media;

    $dados['name'] = $nome;
    $dados['price'] = $preco;
    $dados['media'] = $media;
    $dados['imagem'] = $img;

    return $dados;

From the code $media = Mage::getModel('catalog/product_media_config')->getMediaUrl($product->getThumbnail()); the magento returns the image in my phtml file as follows by the URL:


But in my case, I need to bring the same image from the cache for resizing issues, through this URL:


1 Answer 1


To use images from Magento's cache, you need to apply the logic as seen in most theme templates. Drawing from the base rwd/default theme, you can observe how it works:

File: app/design/frontend/rwd/default/template/catalog/product/list.phtml

<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
        <?php $_imgSize = 210; ?>
        <img id="product-collection-image-<?php echo $_product->getId(); ?>"
             src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($_imgSize); ?>"
             alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />


  • The main utility for processing product images.
  • Uses the init method to generate product image thumbnails.

Example of Use

1    $product = Mage::getModel('catalog/product')->load(1);
2    $image   = Mage::helper('catalog/image')
3        ->init($product, 'small_image')
4        ->resize(50);
5    $imgUrl  = (string) $image;
  • Line 2 constructs and returns an instance of Mage_Catalog_Helper_Image
  • Line 3 creates and attaches an instance of Mage_Catalog_Model_Product_Image
    • The product default small_image is assigned
    • Cache folder directories are assembled
  • Line 4 schedules a resize operation on the image for 50 pixels proportionally
  • Line 5 calls the helper class's internal __toString method
    • This in turn applies scheduled transformations on the image model
    • This is turn uses the underlying image adapter to write the image to cache
  • Great answer! It gave super sure the code, plus all the explanation about what each line does! Withdrawn several doubts, Thank you for all the help and for all the attention! Commented Sep 6, 2017 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.