While building an addition to a Magento webshop, I stumbled upon a couple of problems. In my pursuit to create code which is as fast as possible, I found myself unable to figure out two things.

My (relevant) code is the following:

$rc = Mage::getResourceSingleton('catalog/product');
$productName = $rc->getAttributeRawValue($productId, 'name', Mage::app()->getStore());
$productPrice = $rc->getAttributeRawValue($productId, 'price', Mage::app()->getStore());
$productImage = $rc->getAttributeRawValue($productId, 'image', Mage::app()->getStore());
$productUrl = $rc->getAttributeRawValue($productId, 'url_key', Mage::app()->getStore());

The two things I can't figure out are:

1) How do I retrieve the product URL? url_key doesn't always give the right URL, since not all webshops use canonical URL's, or have the category in the URL.

2) How can I get the product image with a set width/height, instead of the full image? The ->resize() function does not function in this case I recon (since it returns an URL). Added to that, is it possible to get the Base Image, Small Image and Thumbnail separately?

My main goal is to keep the code as fast as possible. I only have a product id, since I loop through the children of a product. I do not wish to use ->load() since that takes way more loading time.


You can do it like this:

$smallImage = $rc->getAttributeRawValue($productId, 'small_image', Mage::app()->getStore());
$imageModel = Mage::getModel('catalog/product_image');

Analogously, you can also do it for "image" and "thumbnail".

| improve this answer | |
  • This seems to work for the regular image, but it selects a wrong folder for the small_image. It seems that the small images all get resized, and it doesn't select the resized variant. Any idea how to solve that? And do you also have a solution for the product URL? – Sander Koedood Jun 25 '14 at 10:50
  • @SanderKoedood For the resizing, add $imageModel->setWidth(150)->setHeight(150)->resize()->getUrl(); – mpaepper Jun 25 '14 at 10:52
  • Instead of 150, use the values you need, of course ;) – mpaepper Jun 25 '14 at 10:56
  • My knowledge does spread that far ;-) but thanks for pointing it out. However, that doesn't change the problem of the site loading the wrong folder. So no image shows when using the small image. – Sander Koedood Jun 25 '14 at 10:57
$productId = 'SOME_PRODUCT_ID';
$product = Mage::getModel('catalog/product')->load($productId);
$productMediaConfig = Mage::getModel('catalog/product_media_config');

$baseImageUrl = $productMediaConfig->getMediaUrl($product->getImage());
$smallImageUrl = $productMediaConfig->getMediaUrl($product->getSmallImage());
$thumbnailUrl = $productMediaConfig->getMediaUrl($product->getThumbnail());
| improve this answer | |
  • If i run above code the browser return blank page. my code : justpaste.it/7hwc9 – Gem May 9 '18 at 8:03

Try below mentioned code to get image URL:

<?php echo Mage::helper('catalog/image')->init($product, 'thumbnail'); ?>
<?php echo Mage::helper('catalog/image')->init($_product, 'small_image') ?>


$productId = 1; // get product ID dynamically instead of 1.
$products = Mage::getModel('catalog/product')->load($productId);
$productUrl = $products->getProductUrl();
$baseImage = $products->getImage(); 
$SmallImage = $products->getSmallImage(); 
$thumbnail = $products->getThumbnail(); 


To get product URL:
| improve this answer | |

You can not do without $products/$product variable:

For product Url:



// you cannot stop this method adding ___store to the URL, even by setting _store_to_url to false


**// you cannot stop this method adding ___store to the URL, even by setting _store_to_url to false // note - see the "using _ignore_category" section below for an arguable bug with using this param**

$product->getUrlInStore(array('_ignore_category' => true));



For product Image:

$productId = 1; 
$products = Mage::getModel('catalog/product')->load($productId);
$products->getImage(); $products->getSmallImage(); 

If you want Image url to use in the tag,

Mage::helper('catalog/image')->init($products, 'small_image')->resize(163,100); // resize function is used to resize image 
Mage::helper('catalog/image')->init($products, 'image')->resize(400,400); 
Mage::helper('catalog/image')->init($products, 'thumbnail')->resize(60,60); 
| improve this answer | |
  • I've got the result of $product->getUrlPath(); already, by getting the url_key. So that is possible without the $product variable. That makes me believe that getting the product image, and resize it, should be possible too, without loading the entire product. (so no use of Mage::getModel('catalog/product')->load($productId);) – Sander Koedood Jun 25 '14 at 9:35

I want to add 1 line of code to the comment of mpaepper, which I think is btw a great solution in specific scenario's. I used it in an loop on a page where I didn't have an product object to load the image through the product model.

When you don't want to load an image with an product object, you can use the approach mpaepper exlpains. Don't forget to save the manual created image when you use this solution. When you save the image object it will create an cache file like the normal Magento images also do.

So the full code should be:

$rc = Mage::getResourceModel('catalog/product'); //empty product model which you only have to load it once. (for the people who want to use this in a loop, put this Resource Model outside the loop.

$smallImage = $rc->getAttributeRawValue($productId, 'small_image', Mage::app()->getStore());
$imageModel = Mage::getModel('catalog/product_image');
$imageModel->saveFile(); //important line to create an cache file of the image
| improve this answer | |

Product's image can be accessed in the following way:

$this->helper('catalog/image')->init($_products, 'small_image');

To resize the image you can add this:

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

To get product url:


Here $_products is the product itself and not the product id;

| improve this answer | |
  • 2
    I have no access to the $_products variable, since that would require me to load the product in it's entirety. And to load all the products I'm looping through would create load times of seconds, which is too much. – Sander Koedood Jun 25 '14 at 9:25
  • why would you not want to get the product data without loading the product? You can add attributeToSelect function to select only the attributes you require rather than loading all the attributes. – Rojan Shrestha Jun 25 '14 at 9:30
  • 1
    Could you show me how you would go at getting the product url and image then? My main purpose for not getting a collection was the assumption that this would be too slow. – Sander Koedood Jun 25 '14 at 10:43
  • I usually load the product and it has never felt slow in any way. I do $_products = Mage::getModel('catalog/product')->loadByAttribute('sku','productsku'); – Rojan Shrestha Jun 25 '14 at 13:01
  • after that follow my answer above – Rojan Shrestha Jun 25 '14 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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