Home » Magento

Magento: Crop image

23 March 2010 2,717 views Popularity: 6% Share/Bookmark

email

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 and thanks for reading.

Related posts:

  1. Magento: Create Watermark Image
  2. Magento: Rotate image
  3. Magento: Get width height of image using Varien_Image class
  4. jQuery: Grey out background and preview image as popup
  5. Magento: Custom function to resize image proportionally
  6. Magento: Resize Image
  7. Magento: Get height and width of Image
  8. jQuery: Animate and Transfer effect with Image
  9. jQuery: Preview Image with Tooltip Effect
  10. jQuery: Preview Image with Zoom Effect
  • http://www.magentonews.co.uk/magnento/magento-crop-image-mukesh-chapagains-blog/ Magento: Crop image | Mukesh Chapagain's Blog | Magento News

    [...] Magento: Crop image | Mukesh Chapagain's Blog [...]

  • http://%/aaglmem.ru Artur

    ……

    свадебные фотоС Наступающей Пасхой < / a…

  • Dipraj

    Hello,

    I am facing issue in product image upload.

    It gives following error.

    Please assist.

    XXXX.JPG (631.94 Kb) Remove
    Unable to create directory ‘/var/www/vhosts/XXXXXX/httpdocs/media/tmp/catalog/product/m/o’.

    Thanks in advance