How to resize Magento e-Commerce category images

Posted by Damodar Bashyal on October 11, 2011

 

So, far I know Magento e-Commerce has no support for image resize other than those for product images. But no need to fear as we can use same image resizing object and customise it to suit our need.

Just follow the following post and you will have image resizer in fraction of time you will spend on researching and writing your own as i had.

Create Folder named "Technooze/Timage" under app/code/community

Now on Under Technooze create 3 more folders Block, etc and Helper.

Under etc folder on config.xml file add below code:

<?xml version="1.0"?>
<!-- 
/**
* @category   Technooze/Modules/magento-how-tos
* @package    Technooze_Timage
* @author     Damodar Bashyal
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
-->
<config>
    <modules>
        <Technooze_Timage>
            <version>0.1.0</version>
        </Technooze_Timage>
    </modules>
    <frontend>
        <routers>
            <timage>
                <use>standard</use>
                <args>
                    <module>Technooze_Timage</module>
                    <frontName>timage</frontName>
                </args>
            </timage>
        </routers>
        <layout>
            <updates>
                <timage>
                    <file>timage.xml</file>
                </timage>
            </updates>
        </layout>
    </frontend>
    <global>
        <helpers>
            <timage>
                <class>Technooze_Timage_Helper</class>
            </timage>
        </helpers>
        <blocks>
            <timage>
                <class>Technooze_Timage_Block</class>
            </timage>
        </blocks>
    </global>
</config>

Now under Block add below code on file Data.php

<?php
/**
* @category   Technooze/Modules/magento-how-tos
* @package    Technooze_Timage
* @author     Damodar Bashyal
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
class Technooze_Timage_Block_Data extends Mage_Core_Block_Template
{
}

Next, under Helper Folder add below code on Data.php

<?php
/**
* @category   Technooze/Modules/magento-how-tos
* @package    Technooze_Timage
* @author     Damodar Bashyal
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
class Technooze_Timage_Helper_Data extends Mage_Core_Helper_Abstract
{
    var
        $width = null,
        $height = null,
        $rawImg = '',
        $img = false,
        $cacheDir = '',
        $cachedImage = '',
        $cachedImageUrl = '',
        $ext = '',
        $bgColor = array(255, 255, 255),
        $imageObj = '',
        $baseUrl = '',
        $placeHolder = false;
    
    public function init($img=false)
    {
        if($img)
        {
            $this->rawImg = $img;
        }
        
        if(empty($this->placeHolder))
        {
            $this->placeHolder = Mage::getDesign()->getSkinUrl() . 'images/catalog/product/placeholder/image.jpg';
        }
        $this->imagePath($this->rawImg);
        
        $this->imageObj = new Varien_Image($this->img);
        
        $path_parts = pathinfo($this->img);
        
        $this->ext = $path_parts['extension'];
        
        $this->cacheDir();
        
        return $this;
    }

    public function resize($width=false, $height=false)
    {
        if($width)
        {
            $this->width = $width;
        }

        if($height)
        {
            $this->height = $height;
        }
        
        $this->cacheIt();
        
        return $this->cachedImageUrl();
    }
    
    public function cachedImageUrl()
    {
        $img = str_replace(BP, '', $this->cachedImage);
        $img = trim(str_replace('\\', '/', $img), '/');
        
        return $this->baseUrl . $img;
    }

    public function cacheIt()
    {
        $this->cachedImage = $this->cacheDir . md5($this->img . $this->width . $this->height) . '.' .$this->ext;
        
        if(file_exists($this->cachedImage))
        {
            return $this->cachedImage;
        }
        
        $this->resizer();
    }

    public function resizer()
    {
        try{
            $this->imageObj->constrainOnly(true);
            $this->imageObj->keepAspectRatio(true);
            $this->imageObj->keepFrame(true);
            $this->imageObj->keepTransparency(true);
            $this->imageObj->backgroundColor($this->bgColor);
            $this->imageObj->resize($this->width, $this->height);
            $this->imageObj->save($this->cachedImage);
        } catch(Exception $e){
            return $e->getMessage();
        }
    }

    public function imagePath($img='')
    {
        $this->baseUrl = str_replace('index.php/', '', Mage::getBaseUrl());
        $img = str_replace($this->baseUrl, '', $img);
        $img = trim(str_replace('/', DS, $img), DS);
        
        $this->img = BP . DS . $img;
        
        if((!file_exists($this->img) || !is_file($this->img)) && !empty($this->placeHolder))
        {
            $this->imagePath($this->placeHolder);
            $this->placeHolder = false;
        }
    }

    public function cacheDir()
    {
        $cache = BP . DS . 'media' . DS . 'catalog' . DS . 'cache' . DS;
        
        if(!is_dir($cache))
        {
            mkdir($cache);
        }
        $this->cacheDir = $cache;
    }
}

Finally, under app/etc/modules add below code on Technooze_Timage.xml

<?xml version="1.0"?>
<!-- 
/**
* @category   Technooze/Modules/magento-how-tos
* @package    Technooze_Timage
* @author     Damodar Bashyal
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
-->
<config>
    <modules>
        <Technooze_Timage>
            <active>true</active>
            <codePool>community</codePool>
        </Technooze_Timage>
    </modules>
</config>

How to Use this module?

It's quite easy actually :) Where you display image just pass through to resize before you display it. e.g.

/*
 * You can pass width and height and much more, see helper for details.
 * echo $this->helper('timage')->init($_category->getImageUrl())->resize(null, 120)
 * echo $this->helper('timage')->init($_category->getImageUrl())->resize(120, null)
 * echo $this->helper('timage')->init($_category->getImageUrl())->resize(120, 120)
 */
<div class="product-image">
    <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>">
        <img src="<?php echo $this->helper('timage')->init($_category->getImageUrl())->resize(null, 120) ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>"/>
    </a>
</div>

Download Full Source Code

Anthony posted on - Sunday 18th of December 2011 12:33:49 AM

Nice tutorial, thanks! Just add small remark: "Now on Under Technooze create 3 more folders Block, etc and Helper." You have to create timage folder under Technooze and then create Block, etc and Helper in it.

Damodar Bashyal posted on - Sunday 18th of December 2011 12:41:48 AM

Thanks Anthony. I have updated the post.

Phill posted on - Friday 4th of January 2013 02:23:30 AM

Found this useful thx

purvi posted on - Saturday 17th of August 2013 07:42:30 AM

hi
i want to resize image in subcategory page in two website...
in one website its working proper but another website its not working proper..

Damodar Bashyal posted on - Tuesday 25th of March 2014 08:24:08 AM

@vivek I don't think you can resize animated gif through this script. It will break animation. Not tested though :)

Praful posted on - Monday 5th of August 2013 09:02:17 AM

Very Useful!!!

Thank you so much!!!

Damodar posted on - Sunday 17th of January 2016 11:57:37 PM

Hi Brian, e.g. see here - app/design/frontend/base/default/template/catalog/category/view.phtml

You can see category image there but has no size defined. So you can use this extension to auto resize category image for you.

inshort, you can use it anywhere where you are showing category image etc.

hopefully that will be enough to solve your issue.

Brian posted on - Sunday 17th of January 2016 11:49:43 PM

I am not sure why I am having such a hard time with this, but I can't figure out how to implement this. I have placed all of the code/files into the correct places on my Magento installation but I don't see any way to access it from the backend/admin area. And I don't see where in the code I can change the height and width of the images for the resize. The readme file (of that last bit of code above, where is that suppose to go? I have been teaching myself Magento for the past year and have always been able to figure out how to do things one way or another, not sure why I can't figure this out.

Vivek posted on - Tuesday 25th of March 2014 08:20:12 AM

Thanks for nice tutorial, just want to ask if i want to use direct image url instead chache url? beascuse i want to show animated gif images, please let me know how can i do this?

Thanks

Damodar Bashyal posted on - Monday 27th of June 2016 01:39:42 AM

@Angela, Please use updated code and doc from here - http://j.mp/resizeImageHub

Angela posted on - Monday 27th of June 2016 01:34:50 AM

I need help. I have created all the files. But I do not understand the last on how to use the module. Can someone help me please?
 
not published on website


QR Code: How to resize Magento e-Commerce category images