Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get original image url Magento (1.6.1.0)

Tags:

magento

I have the following piece of code:

$cProduct = Mage::getModel("catalog/product");
foreach($products_id as $product_id) {
    $product = $cProduct->load($product_id);
    //$products_image[] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).str_replace(Mage::getBaseUrl('media'),"", $product);
    //$products_image[] = $product->getMediaConfig()->getMediaUrl($_product->getData('image'));
    $products_image[] = $product->getImageUrl(); //product's image url
}

As you can see, I've tried several ways to get the original image url. Currently I'm using getImageUrl(), but it retrieves the base image, which is a cropped version. How can I retrieve the original image??

Thanks in advance.

Edit: Seriously appears there isn't a function for it, been Googling for hours straight (also before posting here). So I've written my own function.

function get_original_image_url($base_image_url) {
    $exploded = explode("/", $base_image_url);
    $image_name = $exploded[count($exploded) - 1];

    $original_image_url = "http://yoursitehere.com/media/catalog/product/" . $image_name[0] . "/" .
                           $image_name[1] . "/" . $image_name;

    return $original_image_url;
}

I call it with:

$original = get_original_image_url($product->getImageUrl());

Works for me, though it isn't a nice way to do it.

like image 784
pbond Avatar asked Nov 27 '11 19:11

pbond


2 Answers

You should use the catalog product media config model for this purpose.

<?php

//your code ...

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

Hope this helps.

like image 142
benmarks Avatar answered Sep 29 '22 20:09

benmarks


Other faster way:

$cProduct = Mage::getModel("catalog/product");
$baseUrl = Mage::getBaseUrl('media') . 'catalog/product/';
foreach($products_id as $product_id) {
    $product = $cProduct->load($product_id);
    $products_image[] = $baseUrl . $product->getImage();
}
like image 28
ndlinh Avatar answered Sep 29 '22 20:09

ndlinh