Magento – Get product image url of specific store view with or without https

magento-1.8productproduct-imagesstore-view

  1. i have to call an ajax request from an external website which (the
    simple way) returns the link to an product image of a store view i
    added to the request. The problem is, that the following function
    call always returns the path from the default store view, cause the
    request is to the default store.

    $this->helper('catalog/image')
        ->init($product, 'image')
        ->keepFrame(false)
        ->resize(250);
    
    1. If the calling site is a https site, the website can't show the image, because it's loaded from a non https site.

So there are two questions:

  1. How can i load an image path of a specific store view id?
  2. How can i load the image path with the secure base url?

Thanks in advance for any help!

Best Answer

The quick and dirty way is to strip the protocol designator and turn it into a Protocol Relative URL:

// This assumes $product is from a collection that is constructed including a call to:
// Mage_Catalog_Model_Resource_Category_Collection::setProductStoreId()
$url = $this->helper('catalog/image')
    ->init($product, 'image')
    ->keepFrame(false)
    ->resize(250);
$url = preg_replace('#^https?://#, '//', $url);
Related Topic