Mitech Preloader
Magento

Resize Images in Magento

Hello Guys,

Today i am going to show you how to resize category image in Magento.

Use this code to resize images for category.

[php]
public function getResizeImage(Mage_Catalog_Model_Category $category, $width = 250, $height = 250)
{
// return when no image exists
if (!$category->getImage()) {
return false;
}
// return when the original image doesn’t exist
$imagePath = Mage::getBaseDir(‘media’) . DS . ‘catalog’ . DS . ‘category’
. DS . $category->getImage();
if (!file_exists($imagePath)) {
return false;
}

// resize the image if needed
$rszImagePath = Mage::getBaseDir(‘media’) . DS . ‘catalog’ . DS . ‘category’
. DS . ‘cache’ . DS . $width . ‘x’ . $height . DS
. $category->getImage();
if (!file_exists($rszImagePath)) {
$image = new Varien_Image($imagePath);
$image->resize($width, $height);
$image->save($rszImagePath);
}

// return the image URL
return Mage::getBaseUrl(‘media’) . ‘catalog/category/cache/’ . $width . ‘x’
. $height . ‘/’ . $category->getImage();
}
[/php]

Regards..

blank