Magento获取product image

在做商城时,获取商品图片是非常常见的,而Magento里面已经给我们封好了商品获取的方法,我们直接调用就好:

$product = Mage::getModel('catalog/product')->load($product_id);
$mainImageUrl = (string)Mage::helper('catalog/image')->init($product, 'image')->resize(600, 600);

而这个方法在核心代码中:Mage_Catalog_Helper_Image.php
初始化帮助类来得到图片储存路径:

public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
{
    $this->_reset();
    $this->_setModel(Mage::getModel('catalog/product_image'));
    $this->_getModel()->setDestinationSubdir($attributeName);
    $this->setProduct($product);

    $this->setWatermark(
        Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_image")
    );
    $this->setWatermarkImageOpacity(
        Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_imageOpacity")
    );
    $this->setWatermarkPosition(
        Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_position")
    );
    $this->setWatermarkSize(
        Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_size")
    );

    if ($imageFile) {
        $this->setImageFile($imageFile);
    } else {
        // add for work original size
        $this->_getModel()->setBaseFile($this->getProduct()->getData($this->_getModel()->getDestinationSubdir()));
    }
    return $this;
}

调整图片大小:

public function resize($width, $height = null)
{
    $this->_getModel()->setWidth($width)->setHeight($height);
    $this->_scheduleResize = true;
    return $this;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值