Home » Magento24 February 2010

Magento: Custom function to resize image proportionally

In my previous article to resize image in Magento, I had written direct code to resize any image. Here, I will be writing a simple function to resize image proportionally, which can be kept in the helper file of the module (as helper file can be accessed by any template (.phtml) file).

Here is the custom image resize function. This function will resize image proportionally.

You can write the following code in template (.phtml) file to display the resized image:

Here,

$imageName is the name of the image (e.g. abc.jpg)

$width is the image resize width (e.g. 400)
$height is the image resize height (e.g. 300)
(400 resembles 400px and 300 resembles 300px)

$imagePath is the path where the image is stored/saved. This directory must be inside ‘media’ directory. Like, if we specify ‘xyz/image‘ as $imagePath then our image path must be ‘media/xyz/image/abc.jpg‘ (the $imageName is abc.jpg)

We have defined the width and height to be 400 and 300 respectively. So, a new folder will be created named ‘400×300’. The new resized image will be saved here.

If width is null and height is 300 then the image will be resized proportionally with fixed height of 300px. And the folder name will be ‘x300’.

If width is 400 and height is null then the image will be resized proportionally with fixed width of 400px. And the folder name will be ‘400x’.

If both width and height are null then the image is resized with height ‘100’ and width ‘100’. And the folder name will be ‘100×100’.

Hope this helps. Thanks.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Sumanta Pati

    I want to add some more attribute to customer product like color,size in magento customer partner extension.
    Can You help me by replying.

  • What I want to do is have the product images appear without the white boxes that they currently show up in. If I incorporate the script above to resize the images, will it do that?

    Thanks,
    Jenn

  • Test

    Mardar chod chal hi nih raha hai

  • Test

    Mardar chod chal hi nih raha hai

  • Test

    Mardar chod chal hi nih raha hai

  • Test

    Mardar chod chal hi nih raha hai

  • Test

    Mardar chod chal hi nih raha hai

  • Test

    Mardar chod chal hi nih raha hai

  • Suman

    Nice Post