How To Resize Any Image On Magento

How to resize image on Magento

When you need to set specific size for your image and place it at certain spaces on your website, you should know how to do it correctly. So, let’s look below. 

We have an image that locates in the media folder /media/example.jpg Firstly, we have to initialize the $folderURL variable:

$folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

Secondly, this image URL address:

$imageURL = $folderURL . $fileName;

Remember that the format for $fileName = can be: ‘example.jpg’; or ‘testfolder/example.jpg’; Now, we have to indicate the full path for the existing filename ($basePath) and the name of the file that has to be resized ($newPath). In this particular case they are:

$basePath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . $fileName;

$newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . "resized" . DS . $fileName;

Then, we have to create an instance of Varien_Image class

//$basePath - origin file location

$imageObj = new Varien_Image($basePath);

$imageObj->constrainOnly(TRUE);

$imageObj->keepAspectRatio(FALSE);

$imageObj->keepFrame(FALSE);

/*$width, $height - sizes you need (Note: when keepAspectRatio(TRUE), height would be ignored)*/

$imageObj->resize($width, $height);

//$newPath - name of resized image

$imageObj->save($newPath);

Now, we have to get the URL of the resized image:

  $resizedURL = Mage:getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . "resized" . DS . $fileName;

And at last, we have to make the function resizeImg($fileName, $width, $height = ”)

public function resizeImg($fileName, $width, $height = '')

{

   $folderURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

   $imageURL = $folderURL . $fileName;

 

   $basePath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS .

   $fileName;

   $newPath = Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS .

   "resized" . DS . $fileName;

   //if width empty then return original size image's URL

   if ($width != '') {

       //if image has already resized then just return URL

       if (file_exists($basePath) && is_file($basePath) &&

   !file_exists($newPath)) {

           $imageObj = new Varien_Image($basePath);

           $imageObj->constrainOnly(TRUE);

           $imageObj->keepAspectRatio(FALSE);

           $imageObj->keepFrame(FALSE);

           $imageObj->resize($width, $height);

           $imageObj->save($newPath);

       }

       $resizedURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) .

       "resized" . DS . $fileName;

    } else {

       $resizedURL = $imageURL;

    }

    return $resizedURL;

}

That’s all! We hope that our article will help you with this “challenge”. If you have any questions regarding Magento coding, contact our Sam Ecommerce Magento development specialists and they will answer them! 

Read more here:

Comments (1):
Posted on Wednesday, September 14, 2016 by :
I wanted to thank you for this excellent read!! I deefnitily enjoyed every little bit of it. I have you bookmarked your site to check out the new stuff you post.
Leave a comment:
*Your comment will be published after approval by site administrator.