0

How to Resize Image in custom Category Collection Magento 1.9

 $cats = Mage::getModel('catalog/category')->load(2)->getChildren();
        $catIds = explode(',',$cats);

        $categories = array();
        foreach($catIds as $catId) {
            $category = Mage::getModel('catalog/category')->load($catId); 
            $categories[$category->getName()] = array(
            'url' => $category->getUrl(),
            'description' => $category->getDescription(),
            'img' => $category->getImageUrl()
            );
        }

        ksort($categories, SORT_STRING);

Call Image as below now i want to resize my image.

<img class="img-responsive" src="<?php echo $data['img']; ?>" />

Any one idea how to resize images.

4

Nikul, Magento does not have any default resize function for category like product.

So, you need to use Varien_Image class for this purpose.

Just like below example:

$image = new Varien_Image('/full/fs/path/to/image.jpg');

// you cannot use method chaining with Varien_Image
$image->constrainOnly(false);
$image->keepFrame(true);
// avoid black borders by setting background colour
$image->backgroundColor(array(255,255,255));
$image->keepAspectRatio(true);
$image->resize(216, 139);
$image->save('/full/fs/path/to/save/to.jpg');
|improve this answer|||||
  • From Above my code how to get image resize. – Nikul Jul 13 '16 at 10:35
  • As Per Above code how to get resize in my code.. – Nikul Jul 14 '16 at 10:32
2

Override category model Mage_Catalog_Model_Category file local

after add bellow function override file

/**
    * Retrieve ResizedImage...
    * 
    * @param int $width
    * @param int $height
    * @param int $quality
    * @return string
    */
    public function getResizedImage($width, $height = 135, $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. "category" .DS. "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);
        }

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

    /**
    * Retrieve getResizedThumbnail...
    * 
    * @param int $width
    * @param int $height
    * @param int $quality
    * @return string
    */
    public function getResizedThumbnail($width, $height = 135, $quality = 100)
    {
        if (!$this->getThumbnail())
            return false;

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

        $imageResized = Mage::getBaseDir ( 'media' ) . DS . "catalog" . DS . "product" . DS . "cache" .DS. "category" .DS. "resized" .DS. $this->getThumbnail();// 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);
        }

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

Call Image as below now You want to resize my image:

<?php $categoryId 101; $category = Mage::getModel('catalog/category')->load($categoryId); ?>


<img class="img-responsive" src="<?php echo $category->getResizedThumbnail(135,135); ?>" />



<img class="img-responsive" src="<?php echo $category->getResizedImage(135,135); ?>" />
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.