Magento裁剪图片?

时间:2012-02-29 02:27:59

标签: image magento crop

我发现了这个:http://docs.magentocommerce.com/Varien/Varien_Image/Varien_Image.html#crop

但是我不确定这是否已被弃用或者其他什么因为当我尝试这个时:

echo rawurlencode($this->helper('catalog/image')->init($_product, 'image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->setWatermarkImageOpacity(0)->crop(10, 20, 30, 40)->resize(300, null))

它不起作用并给我这个错误:

Fatal error:  Call to undefined method Mage_Catalog_Helper_Image::crop() in /home/xxxxx/public_html/app/design/frontend/default/xxxxx/template/catalog/product/view.phtml

crop()方法实际上是否可用?如果是的话,我如何使用它来裁剪(不要与调整大小混淆)Magento的产品图片?谢谢!

3 个答案:

答案 0 :(得分:5)

你的错误是假设$this->helper('catalog/image')->init($_product, 'image')返回一个Varien_Image实例,而实际上涉及两个中间类:
Mage_Catalog_Helper_ImageMage_Catalog_Model_Product_Image

catalog/image帮助程序很乱,即使它在最近的版本中已经被清理了一些(例如,没有更多的私有方法)。尽管如此,一些吸气剂在没有真正需要的情况下仍然受到保护 这是我的解决方法:

/* @var $imageHelper Mage_Catalog_Helper_Image */
// Initialize the image helper
$imageHelper = Mage::helper('catalog/image')->init($_product, 'image')
        ->constrainOnly(true)
        ->keepAspectRatio(true)
        ->keepFrame(false)
        ->setWatermarkImageOpacity(0);

// Get the catalog/product_image instance
/* @var $imageModel Mage_Catalog_Model_Product_Image */
$reflection = new ReflectionClass($imageHelper);
$property = $reflection->getProperty('_model');
$property->setAccessible(true);
$imageModel = $property->getValue($imageHelper);

// Initialize the missing values on the image model
// Usually done in Mage_Catalog_Helper_Image::__toString()
if (! $imageModel->isCached())
{
    $getWatermarkMethod = $reflection->getMethod('getWatermark');
    $getWatermarkMethod->setAccessible(true);
    $imageModel->setBaseFile($_product->getImage())
        ->resize()
        ->setWatermark($getWatermarkMethod->invoke($imageHelper));

    // Crop the image using the image processor
    // $imageModel->getImageProcessor() returns a Varien_Image instance
    $imageModel->getImageProcessor()->crop(10, 20, 30, 40);

    // Generate the image according to the set parameters and
    // get the URL while bypassing the helper to avoid reinitialization
    $url = $imageModel->saveFile()->getUrl();
}
echo $url . "\n";

直接使用catalog/product_image模型或Varien_Image会更容易,但这样仍可应用所有Magento水印设置。
无论哪种方式都不干净 我希望助手的助手在未来的版本中公之于众。

答案 1 :(得分:3)

这是替代方法(来自Onlinebizsoft.com的学分) 下面的代码首先查看/ resize目录中可用的图像,如果它不存在,则执行其余操作。

// actual path of image
$_imageUrl = Mage::getBaseDir('media').DS."myimage".DS.$post->getThumbnail();

// path of the resized image to be saved
// here, the resized image is saved in media/resized folder
$imageResized =  Mage::getBaseDir('media').DS."myimage".DS."resized".DS.$post->getThumbnail();

// resize image only if the image file exists and the resized image file doesn't exist
// the image is resized proportionally with the width/height 135px
if (!file_exists($imageResized)&&file_exists($_imageUrl)) :
    $imageObj = new Varien_Image($_imageUrl);
    $imageObj->constrainOnly(TRUE);
    $imageObj->keepAspectRatio(TRUE);
    $imageObj->keepFrame(FALSE);
    $imageObj->resize(135, 135);
    $imageObj->save($imageResized);
endif;

查看网站Resize - Scale Crop images

答案 2 :(得分:0)

您是否尝试过Varien_Image类?

$image = new Varien_Image($img);
$cropped = $image->crop();