How to resize the categories images in Magento?

匿名 (未验证) 提交于 2019-12-03 01:22:02

问题:

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);

回答1:

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


回答2:

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



回答3:

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.



回答4:

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.



回答5:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!