Magento – Magento resize different size images to unique size. Read description.

image-uploadproductproduct-attributeproduct-imagesproduct-list

I am having a catalog with different sized images. Some will be 200×300 some will be 300×150. But All i want to do is to resize all the image to one size which makes me to show in my home page listing.

I am trying the normal magento resize to crop tp 160×210 as follows,

   <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(false)->keepAspectRatio(true)->keepFrame(false)->resize(160, 210); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />

But its cropping images in different size, The images are in random height and widths 160×135, 160×156, 159×210, 150×210

Detailed answers are highly appreciated.

Best Answer

Either set keepFrame to true, so it's always exact 160x210, or set keepAspectRatio to false.

Now it is making sure both width and height won't exceed the values you feed it. But since you don't want a frame, and want to keep your aspect ratio, it has to change both width and height in the same ratio.

<?php 
echo $this->helper('catalog/image')->init($_product, 'small_image')
    ->constrainOnly(false)
    ->keepAspectRatio(true)
    ->keepFrame(true)
    ->resize(160, 210);
?>

Edit: For cropping you need to use Varien_Image. Unfortunately the Mage_Catalog_Helper_Image doesn't offer a method to return the Varien_Image class. So you need to make a custom module to make it possible to crop.

See https://stackoverflow.com/questions/9492770/magento-crop-image for an example.

Basically it says you need to make your own helper and extend Mage_Catalog_Helper_Image, so you can use $this->_getModel()->getImageProcessor()->crop()

Related Topic