Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

Get original image url Magento (1.6.1.0)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...