如何从产品系列中获取产品图像网址?

时间:2013-12-12 18:46:52

标签: magento

我需要为XML Feed重新调整图像大小。我正在循环使用产品,但在尝试获取重新调整大小的图像的URL时,我遇到了问题。

foreach ($collection as $_product) {

$MainImage = Mage::helper('catalog/image')->init($_product, 'small_image')->constrainOnly(false)->keepAspectRatio(true)->keepFrame(true)
->keepTransparency(true)->resize(300, 150);

$arr['Image'] = $_product->getSmallImageUrl();
$arr['MainImage'] = $MainImage;
$arr['Name'] = $_product->getName();

}

如果我回显$ MainImage,它会返回一个URL,但由于某种原因,它不喜欢在字符串中。

基本上,如果我然后回显数组,它不显示图像的URL,它只是空白。

有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:0)

你试过吗

  

$ arr ['MainImage'] =(字符串)Mage :: helper('catalog / image') - > init($ _ product,'small_image');?

答案 1 :(得分:0)

如果你看看@ /app/design/frontend/default/base/template/catalog/product/view/media.phtml

$_helper = Mage::helper('catalog/output');

....   
foreach ($collection as $_product) {
    $MainImage = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(410,420);
    $MainImage = $_helper->productAttribute($_product, $MainImage, 'small_image');

或将返回值转换为字符串

$MainImage = (string)Mage::helper('catalog/image')->init($_product, 'small_image')->constrainOnly(false)->keepAspectRatio(true)->keepFrame(true)->keepTransparency(true)->resize(300, 150);