Here is the custom image resize function. This function will resize image proportionally.
/**
* Resize Image proportionally and return the resized image url
*
* @param string $imageName name of the image file
* @param integer|null $width resize width
* @param integer|null $height resize height
* @param string|null $imagePath directory path of the image present inside media directory
* @return string full url path of the image
*/
public function resizeImage($imageName, $width=NULL, $height=NULL, $imagePath=NULL)
{
$imagePath = str_replace("/", DS, $imagePath);
$imagePathFull = Mage::getBaseDir('media') . DS . $imagePath . DS . $imageName;
if($width == NULL && $height == NULL) {
$width = 100;
$height = 100;
}
$resizePath = $width . 'x' . $height;
$resizePathFull = Mage::getBaseDir('media') . DS . $imagePath . DS . $resizePath . DS . $imageName;
if (file_exists($imagePathFull) && !file_exists($resizePathFull)) {
$imageObj = new Varien_Image($imagePathFull);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(TRUE);
$imageObj->resize($width,$height);
$imageObj->save($resizePathFull);
}
$imagePath=str_replace(DS, "/", $imagePath);
return Mage::getBaseUrl("media") . $imagePath . "/" . $resizePath . "/" . $imageName;
}
You can write the following code in template (.phtml) file to display the resized image:
<img src="<?php echo Mage::helper('moduleName')->resizeImage('abc.jpg', 400, 300, 'xyz/image'); ?>" alt="resized image" />
No comments:
Post a Comment