B-219 Sec-55 Noida, India
+918010221733

Image Resize In Magento

Magento has its own strong Image resize functionality, Once you have uploaded the products images then it resizes images and save it accordingly, Magento resizes images into the Image,Small Image and Thumbnails and display it as per the frontend requirements. But In Magento you can resize image with fixed height and variable width Or fixed width and variable height according to your requirements. Codes are given below:

Case 1. Suppose you want to resize image with the fixed width of 450px

<?php
echo $this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(450,null)
?>

Case 2. Suppose you want to resize image with the fixed height of 450px

<?php
echo $this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(null,450)
?>

Case 3. Suppose you want to resize resize image proportionally and not let the image be greater than height and width specified.

<?php
echo $this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(400,400)
?>

Now here are the description of the functions used:

1. constrainOnly(bool $flag)
It is just for to make sure that image picture will not be bigger than its actual size. Its only applicable before calling resize(), otherwise it is false by default.

2. keepAspectRatio(bool $flag)
It is just for to make sure that image picture width/height will not be distorted. Its applicable before calling resize(), It is true by default.

3. keepFrame(bool $flag)
It is just for to make sure that image will have dimensions, set in $width/$height. Its applicable before calling resize(), Not applicable, if keepAspectRatio(false).
You can also resize the images by using the Varien_Image class. This is most suitable when you are not resizing the catalog product images.

 Step 1. Suppose you want to resize a image which is not the catalog product image, but you have the actual image path:

$actualimageUrl = Mage::getBaseDir(‘media’).DS.”EWA”.DS.$post->getThumbnail();

// actual path of image

Step 2. Now I am going to save the resized image into the EWA/resized

$ResizedimageUrl = Mage::getBaseDir(‘media’).DS.”EWA”.DS.”resized”.DS.$post->getThumbnail();

Step 3. Condition to check and resize image only if the image file exists and the resized image file doesn’t exist

Here we want to resize the image proportionally with the same height and width suppose 100px;

<?php
if (!file_exists($imageResized)&&file_exists($actualimageUrl)) :
$imageObj = new Varien_Image($actualimageUrl);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(TRUE);
$imageObj->keepFrame(FALSE);
$imageObj->resize(100, 100);
$imageObj->save($ResizedimageUrl);
endif;
?>

 Step 4. Now you can display the resized image by using the syntax given below:

<img src=”<?php echo Mage::getBaseUrl(‘media’).”EWA/resized/”.$post->getThumbnail(); ?>” />

Full Code are given below:

<?php
$actualimageUrl = Mage::getBaseDir(‘media’).DS.”EWA”.DS.$post->getThumbnail();

// actual path of image

$ResizedimageUrl = Mage::getBaseDir(‘media’).DS.”EWA”.DS.”resized”.DS.$post->getThumbnail();

 //save the resized image into the EWA/resized

if (!file_exists($imageResized)&&file_exists($actualimageUrl)) :
$imageObj = new Varien_Image($actualimageUrl);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(TRUE);
$imageObj->keepFrame(FALSE);
$imageObj->resize(100, 100);
$imageObj->save($ResizedimageUrl);
endif;
?>

<img src=”<?php echo Mage::getBaseUrl(‘media’).”EWA/resized/”.$post->getThumbnail(); ?>” />


(Visited 109 times, 1 visits today)

Leave a reply