Resize image in Magento using Varien_Image class

April 10th, 2012 by laeeq | No comments

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.

  1. $_imageUrl = Mage::getBaseDir(‘media’).DS.$image;
  2. $imageResized = Mage::getBaseDir(‘media’).DS.“resized”.$image;
  3. if (!file_exists($imageResized)&&file_exists($_imageUrl)) :
  4. $imageObj = new Varien_Image($_imageUrl);
  5. $imageObj->constrainOnly(TRUE);
  6. $imageObj->keepAspectRatio(TRUE);
  7. $imageObj->keepFrame(FALSE);
  8. $imageObj->resize(140, 140);
  9. $imageObj->save($imageResized);
  10. endif;

Hope this article will work for you!.

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Get an array of billing addresses and shipping addresses in Magento
  • Duplicate Data Issue with Custom Magento Collection
  • Tips To Improve Your Ecommerce Site’s Security
  • Magento: Display New Products from Specific Category On Home Page
  • Dealing with Magento Model or Collections
  • Magento: Display New Products On Home Page
  • Magento: Display More Than 5 New Products
  • Override Controllers in Magento
  • Improve the performance of your Magento store
  • Magento Released Community Edition 1.7.0.1
  • Write custom title, keywords and description in Magento module
  • Moving Magento site from development to live server
  • Display Related products on product details page in Magento
  • Magento – Models, resource models, and collections
  • Magento – Add thumbnail images to Magento admin grid
  • Simple url rewrite in magento
  • Generate CSV file in Magento
  • AJAX requests in Magento
  • Category Navigation Listings in Magento
  • Magento- Create a Drop-Down of Countries
  •  

     

    1. No comments yet.
    1. No trackbacks yet.