a1anm
a1anm

Reputation: 1629

Magento resize image

I am using the below code to display product images by their label:

$productId = $this->getProduct_id(); 
$_product = Mage::getModel('catalog/product')->load($productId);


src="<?php echo $_product->getMediaGalleryImages()->getItemByColumnValue('label', 'mylabel')->getUrl(); ?>"

How can I resize the image?

Upvotes: 0

Views: 2279

Answers (1)

Shivam
Shivam

Reputation: 2443

// Image name

$image = "";

    // actual image path

//$imageUrl = Mage::getBaseDir('media'). DS .'test'. DS .$image;

in your case image url is

$imageUrl="<?php echo $_product->getMediaGalleryImages()->getItemByColumnValue('label', 'mylabel')->getUrl(); ?>"


    // Give resized image path to be saved
    // here, the resized image will save in media/test/resized folder

$imageResized = Mage::getBaseDir('media'). DS .'test'. DS .'resized'. DS .$image;

    // image will resize only if the image file exists and the resized image file doesn't exist



 if (!file_exists($imageResized) && file_exists($imageUrl))
    {
        $imageObj = new Varien_Image($imageUrl);
        $imageObj->constrainOnly(TRUE);
        $imageObj->keepAspectRatio(TRUE);
        $imageObj->keepFrame(FALSE);
        $imageObj->resize(300, null);
        $imageObj->save($imageResized);
    }

hope this will help you

Upvotes: 1

Related Questions