Magento: Crop image

Here is a quick tip to crop image to any degree of your choice in Magento. The crop function of Varien_Image does the magic :)

Here goes the code:


$mainImage = Mage::getBaseDir('media') . DS . 'test' . DS . 'image.jpg';
 
$image = new Varien_Image($mainImage);

// crop image 
// crop($top=0, $left=0, $right=0, $bottom=0)
$image->crop(10, 10, 10, 10);
$image->save(Mage::getBaseDir('media'). DS . 'test' . DS . 'new.jpg');

Code Explanation

In the code above, you can see that I have an image named image.jpg inside media/test/ directory.

At first, I have cropped the image by 10px from top, left, bottom, and right side.
The new cropped image is saved inside media/test/ directory as new.jpg.

Hope this helps. Thanks.