Wednesday, January 22, 2014

Magento : Get and Resize Category Images


Override following file app/code/core/Mage/Catalog/Model/Category.php and define following method

public function getCategoryImage(Mage_Catalog_Model_Category $category, $width = 250, $height = 250, $quality = 100)
{
    // 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)) {
        $imageObj = new Varien_Image($imagePath);
$imageObj->constrainOnly ( true );
$imageObj->keepAspectRatio ( true );
$imageObj->keepFrame ( false );
$imageObj->quality ( $quality );
$imageObj->resize($width, $height);
        $imageObj->save($rszImagePath);
    }
    // return the image URL
    return Mage::getBaseUrl('media') . '/catalog/category/cache/' . $width . 'x'
         . $height . '/' . $category->getImage();
}

1 comment:

  1. In category view.phtml file what will be the code

    ReplyDelete