2012-01-03 73 views
4

如何调整在Magento的图像类别?我用下面的代码来调整产品图片,但我不能用它来显示图像类别:如何调整Magento中的分类图像?

$this->helper('catalog/image')->init($_product, 'small_image')->resize(170); 

回答

5

如果我没有记错的话,应该是:

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

这是新的代码。如果您在使用哪个扩展名之前告诉我,我们很快解决了。 如果我没有记错,您使用的模板怪兽目录意象的延续。所以,扩展中有一个函数,如下所示。 应用程序/设计/前端:

// app/design/frontend/default/default/template/easycatalogimg/homepage.phtml 
<?php echo Mage::helper('easycatalogimg/image')->resize($imageUrl, $width , $height) ?> 
+1

因为我用我不能用这种 “法师:: getModel( '目录/类') - >负载($ _分类 - >的getId());”拿到类别实例..上面的代码工作的单品.. – balanv 2012-01-03 12:20:08

+0

@balanv尝试新的代码,请。 – 2012-01-03 12:50:26

+0

我试过这个,但无法使用这个..当我使用代码$ this-> helper('catalog/image') - > init($ cur_category,'thumbnail',$ image-> getFile()) - > resize(100,100); 它显示错误,如 “调用成员函数getFile()在/app/design/frontend/default/default/template/easycatalogimg/subcategories.phtml非对象” – balanv 2012-01-04 10:49:50

0

如果需要调整类别的图像,因为它使缩小到475px宽,你可以通过删除“WIDTH =‘475’”从下面的模板做/default/default/template/catalog/category/view.phtml

您可能还需要关闭或清除后端的缓存:系统 - >缓存管理 - >映像缓存 - >清除。

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

+0

谢谢德尔齐.. – balanv 2012-01-04 11:28:34

2

如果你想调整类别的图像,here是基于核心图像尺寸调整免费模块。在我自己挣扎的时候,最终创建了这个扩展来调整类别图像的大小,使其与产品图像大小调整相同。

+1

你想把helper的第33行替换为:$ this-> placeHolder = Mage :: getDesign() - > getSkinUrl('images/catalog/product/placeholder/image.jpg');'如果主题文件夹中没有占位符,则避免无限嵌套(通过使用上述代码,magento将回退到默认占位符) 。否则伟大的代码,感谢分享 – srgb 2013-03-09 12:33:03

+0

@Netismine谢谢你,并完成。 – 2013-03-11 20:09:59

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

“/>

更多的澄清see here

0

我意识到,我迟到了,但我有机会在这个看起来最近使用init()来调整产品如在问题描述,但你不应该使用相同的函数来调整单个图像,通过函数,其中所述图像本身(第三个参数)是任选的值的图像是完全没有问题。

这里是我做了什么调整图像大小,这是任何图像:

$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 

如果你不想保存在media目录你的形象,那么你可以使用Varien_Image对象来实现这一目标:

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

save()采取的第一个参数是您希望新的文件保存和第二个参数是新文件的名称的目录。

其步骤为init功能从处理与产品本身一堆逻辑做什么一边相似。