Resize image in Magento using Varien_Image class
Sometimes we needs to resize images according to requirement. Actaully Magento has a library which can resize image in height or width. You can resize product images easily using catalog helper. sometimes, you want to resize images which are not uploaded for product. Here, I’ll describe how to resize images, such as needing to resize a custom image and display it on the site in special sized areas. Let’s proceed…
Below is the code to resize image using Varien_Image class. This code resizes image from a specific directory and save resized image in another directory.
- $_imageUrl = Mage::getBaseDir(‘media’).DS.$image;
- $imageResized = Mage::getBaseDir(‘media’).DS.“resized”.$image;
- if (!file_exists($imageResized)&&file_exists($_imageUrl)) :
- $imageObj = new Varien_Image($_imageUrl);
- $imageObj->constrainOnly(TRUE);
- $imageObj->keepAspectRatio(TRUE);
- $imageObj->keepFrame(FALSE);
- $imageObj->resize(140, 140);
- $imageObj->save($imageResized);
- endif;
Hope this article will work for you!.
Follow @phpzag
