Wednesday, January 11, 2012

Category Image Re-size

Hi,

We don't have any function to re-size the category image, i saw one post where it clearly mention how to do it

call this code where you need to display

$_category=Mage::getModel('catalog/category')->load($categoryId());
$_imgUrl = $_category->getResizedImage(70,70);

and extend category model to your local system or your custom module
XML

<global>
<models>
<catalog>
<rewrite>
<category>NameSpace_Modulename_Model_Category</category>
</rewrite>
</catalog>
</models>
</global>


in side model folder add this file Category.php and paste this code


<?php

class NameSpace_Modulename_Model_Category extends Mage_Catalog_Model_Category
{

public function getResizedImage($width, $height = null, $quality = 100) {

if (! $this->getImage ())
return false;

$imageUrl = Mage::getBaseDir ( 'media' ) . DS . "catalog" . DS . "category" . DS . $this->getImage ();
if (! is_file ( $imageUrl ))
return false;

$imageResized = Mage::getBaseDir ( 'media' ) . DS . "catalog" . DS . "product" . DS . "cache" . DS . "cat_resized" . DS . $this->getImage ();// Because clean Image cache function works in this folder only
if (! file_exists ( $imageResized ) && file_exists ( $imageUrl ) || file_exists($imageUrl) && filemtime($imageUrl) > filemtime($imageResized)) :
$imageObj = new Varien_Image ( $imageUrl );
$imageObj->constrainOnly ( true );
$imageObj->keepAspectRatio ( true );
$imageObj->keepFrame ( false );
$imageObj->quality ( $quality );
$imageObj->resize ( $width, $height );
$imageObj->save ( $imageResized );
endif;

if(file_exists($imageResized)){
return Mage::getBaseUrl ( 'media' ) ."/catalog/product/cache/cat_resized/" . $this->getImage ();
}else{
return $this->getImageUrl();
}

}

}

No comments:

Post a Comment