Resize image in Magento

Magento has a library which can resize image in height or width. You can resize product images easily using catalog helper. But sometimes, you want to resize images which are not uploaded for product. You can use them when developing your module.
Here is the code you find in media.phtml of catalog module. This code is used to resize product image into 265px X 265px

$this->helper('catalog/image')->init($_product, 'image')->resize(265)

Spent a little time to study the code, I find out a way to resize image using Varien_Image class. The following 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->resize(135, 135);

Hope this small article can help you develop your own gallery module.

4 thoughts on “Resize image in Magento”

4 Responses to Resize image in Magento