5

How to resize the categories images in Magento? I used the following code to resize product images, but I'm not able to use it for showing categories images:

$this->helper('catalog/image')->init($_product, 'small_image')->resize(170);
Knase
  • 1,224
  • 14
  • 23
balanv
  • 10,686
  • 27
  • 91
  • 137

5 Answers5

5

If I am not mistaken, it should be :

init($_product, 'small_image')->resize(100,100);

// single parameter work with 'image'
init($_product, 'image')->resize(100);

// How about this
$this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $image->getFile())->resize(100,100);

Here is the new code. If you tell me before which extension used, we were solve quickly. If I am not mistaken, you used Template Monster Catalog Image Extension. So, there is a function inside of the extension, like below.

// app/design/frontend/default/default/template/easycatalogimg/homepage.phtml
<?php echo Mage::helper('easycatalogimg/image')->resize($imageUrl, $width , $height) ?>
Oğuz Çelikdemir
  • 4,990
  • 4
  • 30
  • 56
  • 1
    I cannot use this because i am using "Mage::getModel('catalog/category')->load($_category->getId());" to get the category instance.. above code works for individual products.. – balanv Jan 03 '12 at 12:20
  • I have tried this, but cannot able to use this.. When i use the code $this->helper('catalog/image')->init($cur_category, 'thumbnail', $image->getFile())->resize(100,100); it shows error like "Call to a member function getFile() on a non-object in /app/design/frontend/default/default/template/easycatalogimg/subcategories.phtml" – balanv Jan 04 '12 at 10:49
  • Great!!! it works just like that Oğuz Çelikdemir... +1 for responding the question with updates... Thanks – balanv Jan 04 '12 at 11:26
3
 <?php
               $_file_name = $cat->getThumbnail(); // Here $cat is category data array                
$_media_dir = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
                $cache_dir = $_media_dir . 'resize' . DS; // Here i create a resize folder. for upload new category image

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 imagename
 ?>

" />

For more clarification see here

user3146094
  • 429
  • 5
  • 3
2

If you want to resize category images, here is a free module based on core image resize. As I was struggling myself so, ended up creating this extension to resize category images with ease same as product image resizing.

honk
  • 9,137
  • 11
  • 75
  • 83
Damodar Bashyal
  • 931
  • 3
  • 17
  • 31
  • 1
    you want to replace line 33 of Helper to this: `$this->placeHolder = Mage::getDesign()->getSkinUrl('images/catalog/product/placeholder/image.jpg');` to avoid infinite nesting if there is no placeholder in theme folder (by using the code from above magento will fall back to default placeholder). Otherwise great code, thanks for sharing – srgb Mar 09 '13 at 12:33
1

I realize that I'm late to the party but I had a chance to look at this recently and using init() to resize product images is perfectly fine as described in the question but you shouldn't have to use the same function to resize a single image, through a function where the image itself (third parameter) is an optional value.

Here's what I've done to resize the image, this is for any image:

$model = Mage::getModel('catalog/product_image');
$model->setBaseFile('test.png');
$model->setWidth(100);
$model->resize();
$model->saveFile();
echo $model->getUrl();
// generates the following url: http://example.com/media/catalog/product/cache/1//9df78eab33525d08d6e5fb8d27136e95/test.png

If you don't want to save your image in the media directory then you can use Varien_Image object to achieve that:

$varienImage = new Varien_Image('test.png');
$varienImage->resize(100);
$varienImage->save('var', 'test2.png');

save() takes the first parameter as the directory where you'd like the new file to be saved and second parameters is the name of the new file.

The steps are similar as to what the init function does aside from dealing with a bunch of logic with the product itself.

dchayka
  • 1,291
  • 12
  • 20
0

If you need to resize the category image as it keeps shrinking to 475px wide, you can do so by deleting the 'width="475"' from the following template: app/design/frontend/default/default/template/catalog/category/view.phtml

You might also need to turn off or clear the cache in the backend at: System --> Cache Management --> Image Cache --> Clear.

From: http://www.pridedesign.ie/content/magento-resize-category-image

sondoha
  • 156
  • 6