Magento resize category image

Magento resize category image custom.



$_file_name = $cat->getThumbnail(); // Here $cat is category data array 
$_media_dir = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;

 // Here i create a resize folder. for upload new category image
$cache_dir = $_media_dir . 'resize' . DS;

if (file_exists($cache_dir . $_file_name)) {

 $catImg =Mage::getBaseUrl('media') .  'catalog' . DS . 'category' . DS . 'resize' . DS . $_file_name;

} elseif (file_exists($_media_dir . $_file_name)) {

     if (!is_dir($cache_dir)) {
       mkdir($cache_dir);
    }

   $_image = new Varien_Image($_media_dir . $_file_name);
   $_image->constrainOnly(true);
   $_image->keepAspectRatio(false);
   $_image->keepFrame(false);
   $_image->keepTransparency(true);
   $_image->resize(224, 174); // change image height, width
   $_image->save($cache_dir . $_file_name);

   $catImg = Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . 'resize' . DS . $_file_name;

}


 echo  $catImg ; // display resize category thumbnail image name

<img src="<?php echo $catImg;  ?>"  />

Leave a Reply