Magento has own resize engine that can resize catalog image on your own desire width and height.That means you can resize image with fixed width and variable height or you can set fixed height with variable width.Also you can resize image with no white background in magento means you remove white background from your image background.So to fit or remove white background use keepFrame(false).

please take a quik look of some functions that I have used in below coding like :

1)constrainOnly :

constrainOnly(bool $flag) : it guarantees that image will not be bigger than it was.we can use it before resize() and default value is true.

2)keepAspectRatio :

keepAspectRatio(bool $flag) : it guarantees that image width/height will not be contorted.we can use it before resize() and default value is true.

3)keepFrame :

keepFrame(bool $flag) : it guarantees that image will have dimension and white background can managed if image is not fit with desired dimention.That means set keepFrame(false) remove the background and set image with desired width/height

for fixed with I have width : 500

<img src="<?php echo $this-/>helper('catalog/image')->init($_product, 'image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(500,null); ?>"/>

for fixed with I have height : 500

<img src="<?php echo $this-/>helper('catalog/image')->init($_product, 'image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(null,500); ?>"/>

Also you can specify the height and width image to image would not exceed that you have mentioned.

<img src="<?php echo $this-/>helper('catalog/image')->init($_product, 'image')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(500,500); ?>"/>

You can resize image through Varien_Image if you are using code in custom script or any where else.
I am here checked if there is rsized image or not if not I am creating a new resized image propertioanly with heght and width with Varien_Image.

<?php
$imageUrl = Mage::getBaseDir('media').DS."customimage".DS."testimage.jpg";//path of image
$imageResized = Mage::getBaseDir('media').DS."customimage".DS."resized".DS."testimage.jpg";//resized image path

if (!file_exists($imageResized)&&file_exists($_imageUrl)) {
    $imageObj = new Varien_Image($_imageUrl);
    $imageObj->constrainOnly(TRUE);
    $imageObj->keepAspectRatio(TRUE);
    $imageObj->keepFrame(FALSE);
    $imageObj->resize(135, 135);
    $imageObj->save($imageResized);
}
?>

Sometimes you have only image url like http://jyotiranjan.in/customimage/testimage.jpg so you want to resize that.For that lets how I am resizing that image.Now I am creating a resized folder and have to save that resized image in that folder.

<?php
$imageUrl = 'http://jyotiranjan.in/customimage/testimage.jpg';
// create folder
if(!file_exists("./media/customimage/resized"))
mkdir("./media/customimage/resized",0777);

// get image name
$imageName = substr(strrchr($imageUrl,"/"),1);

// resized image path (media/catalog/category/resized/IMAGE_NAME)
$imageResized = Mage::getBaseDir('media').DS."customimage".DS."resized".DS.$imageName;

// changing image url into direct path
$dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($imageUrl,'/media'));

// if resized image doesn't exist, save the resized image to the resized directory
if (!file_exists($imageResized)&&file_exists($dirImg)){
    $imageObj = new Varien_Image($dirImg);
    $imageObj->constrainOnly(TRUE);
    $imageObj->keepAspectRatio(TRUE);
    $imageObj->keepFrame(FALSE);
    $imageObj->resize(120, 120);
    $imageObj->save($imageResized);
}

$newImageUrl = Mage::getBaseUrl('media')."customimage/resized/".$imageName;
?>
Resize image with no white background in magento
Tagged on:                             

3 thoughts on “Resize image with no white background in magento

  • June 12, 2015 at 10:49 am
    Permalink

    Thanks Jyotiranjan, for sharing this solution. It’s an easy way to re-size the images with no white background in Magento.

    Check this Background Image Switcher Extension by FME: http://www.fmeextensions.com/magento-background-images.html

    This extension allows you to configure different background images for different events like Christmas, holidays, etc. The images are automatically changed according to the pre-configured store.

    Reply
  • May 4, 2016 at 9:03 pm
    Permalink

    It is very stupid that every one writing the same code everywhere without telling which file to edit. Guys stop copieng it over and over again

    Reply
    • May 5, 2016 at 7:59 pm
      Permalink

      I can see you have got totally frustrated.

      Most of time this code implements in list.phtml if you have custom theme with custom package then that file will be at (app/design/frontend/[custom_package]/[custom_theme]/template/catalog/product/) else that file is at by default in (app/design/frontend/base/default/template/catalog/product/) .

      open list.phtml file and check line no near to 50 for list mode and 95 for grid mode.

      The code and logic is described here in blog is for managing image in magento not any other framework. So you can use anywhere in magento for managing images as you like.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *