`
天梯梦
  • 浏览: 13763948 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

magento图片处理 Customize Magento’s Image Resize Functionality

 
阅读更多

1. Customize Magento’s Image Resize Functionality

 

Need to remove the white border around your images? Don't want everything to be square? Here is how to customize the ->resize functionality in Magento.Here is what the default image resize code looks like:

<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(350) ?>
 

The problem is this will always give you a 350 x 350 pixel square. If your image is rectangular, you will get a white frame around it to make it square. The resize() command can be quickly and easily customized to work better with rectangular images.

 

->constrainOnly(true) This will not resize an image that is smaller than the dimensions inside the resize() part.

->keepAspectRatio(true) This will not distort the height/width of the image.

->keepFrame(false) This will not put a white frame around your image.

 

Here is what your image code would look like with all these set:

<?php echo $this->helper('catalog/image')->init($_product, 'image')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->resize(350, null) ?>
 

This would resize your images to a max 350 width and constrain the height. If your image is taller than it is wide, you will end up with a nicely resized vertical image.

 

Here are the various places that images are used:

/app/design/frontend/default/yourtheme/catalog/product/view/media.phtml   (displays the image on your product view page)

/app/design/frontend/default/yourtheme/catalog/product/list.phtml   (displays the image on category view)

 

This has helped us out many times. Let us know in the comments if you use it!

 

from: http://www.magthemes.com/magento-blog/customize-magentos-image-resize-functionality/

 

2. Magento: Resize Image

 

You can resize image with fixed height and variable width. Or, you can resize with fixed width and variable height. Following code shows how you do it in Magento.

 

Fixed width of 600px

<?php echo $this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(600,null)
?>
 

Fixed height of 600px

<?php echo $this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(null,600)
?>
 

The following code will resize image proportionally and not let the image be greater than height and width specified.

<?php echo $this->helper('catalog/image')->init($_product, 'image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize(400,400)
?>
 

Description of the functions used above:-

 

constrainOnly(bool $flag)
Guarantee, that image picture will not be bigger, than it was. Applicable before calling resize() It is false by default

 

keepAspectRatio(bool $flag)
Guarantee, that image picture width/height will not be distorted. Applicable before calling resize() It is true by default.

 

keepFrame(bool $flag)
Guarantee, that image will have dimensions, set in $width/$height. Applicable before calling resize() Not applicable, if keepAspectRatio(false).

 

You can resize image with Varien_Image class as well. This is most suitable when you are resizing image that is not related with catalog product.

// actual path of image
$imageUrl = Mage::getBaseDir('media').DS."myimage".DS.$post->getThumbnail();
 
// path of the resized image to be saved
// here, the resized image is saved in media/resized folder
$imageResized = Mage::getBaseDir('media').DS."myimage".DS."resized".DS.$post->getThumbnail();
 
// resize image only if the image file exists and the resized image file doesn't exist
// the image is resized proportionally with the width/height 135px
if (!file_exists($imageResized)&&file_exists($_imageUrl)) :
    $imageObj = new Varien_Image($_imageUrl);
    $imageObj->constrainOnly(TRUE);
    $imageObj->keepAspectRatio(TRUE);
    $imageObj->keepFrame(FALSE);
    $imageObj->resize(135, 135);
    $imageObj->save($imageResized);
endif;
 

You can use the resized image now as:

<img src="<?php echo Mage::getBaseUrl('media')."myimage/resized/".$post->getThumbnail() ?>" />
 

Another Scenario

Suppose, you have an image link. Now, you want to resize it. The image might be from any place. In this example, I am supposing the image link to be like http://localhost/magento/media/catalog/category/test_image.jpg

 

Now, I have to resize it. To do so, I will create a directory called resized inside media/catalog/category . And, I will save the resized file into the newly created resized directory.

// my sample image
 
$imageUrl = "http://localhost/magento/media/catalog/category/test_image.jpg";
 
// create folder
 if(!file_exists("./media/catalog/category/resized"))     mkdir("./media/catalog/category/resized",0777);
 
 // get image name
 $imageName = substr(strrchr($imageUrl,"/"),1);
 
 // resized image path (media/catalog/category/resized/IMAGE_NAME)
 $imageResized = Mage::getBaseDir('media').DS."catalog".DS."category".DS."resized".DS.$imageName;
 
 // changing image url into direct path
 $dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($imageUrl,'/media'));
 
 // if resized image doesn't exist, save the resized image to the resized directory
 if (!file_exists($imageResized)&&file_exists($dirImg)) :
 $imageObj = new Varien_Image($dirImg);
 $imageObj->constrainOnly(TRUE);
 $imageObj->keepAspectRatio(TRUE);
 $imageObj->keepFrame(FALSE);
 $imageObj->resize(120, 120);
 $imageObj->save($imageResized);
 endif;
 
 $newImageUrl = Mage::getBaseUrl('media')."catalog/category/resized/".$imageName;
 

Displaying newly created resized image.

<img src="<?php echo Mage::getBaseUrl('media')."catalog/category/resized/".$newImageUrl ?>" />
 

You can check other function for Varien_Image at
http://docs.magentocommerce.com/Varien/elementindex_Varien_Image.html

 

from: http://blog.chapagain.com.np/magento-resize-image/

 

 

 

分享到:
评论

相关推荐

    magento图片延时加载插件

    总结来说,“magento图片延时加载插件”是提升Magento电商网站性能的有效工具,它通过优化图片加载策略,提高了页面加载速度,改善了用户体验。正确的安装和配置是实现这些好处的关键,而持续的维护和更新则能确保...

    Magento批量处理订单

    Magento提供了批量处理订单的功能,这极大地提高了效率和准确性。本文将详细探讨Magento批量处理订单的流程、方法以及相关的知识点。 1. **批量发货**: 在Magento后台,商家可以批量选择已完成的订单并进行发货...

    magento 后台订单显示图片插件

    在Magento的后台管理系统中,商家可以处理各种订单、管理产品、跟踪库存等。然而,原生的Magento系统并未提供在后台订单详情页面直接查看商品图片的功能。"magento 后台订单显示图片插件"就是为了弥补这个不足而设计...

    Magento 常用方法和插件

    例如,`Mage_Catalog`模块处理产品展示,`Mage_Checkout`处理购物车和结账流程。理解如何创建、注册和管理这些模块,以及它们之间的依赖关系,对于开发者来说至关重要。 其次,Magento的事件观察者机制是其灵活性的...

    Magento Beginner's Guide

    ### Magento初学者指南知识点概述 #### 一、Magento开源电子商务平台简介 Magento是一款功能强大的开源电子商务平台,旨在帮助用户创建动态且功能全面的在线商店。它不仅提供了丰富的特性和工具来满足不同规模的...

    Magento批量产品多图上传

    2. **图片处理**:Magento会自动处理上传的图片,包括缩放、裁剪以适应预设的尺寸,确保在前端展示时的统一性。 3. **优化性能**:批量上传能有效降低服务器负载,提高数据处理速度,特别是在处理大量图片时。 4. ...

    magento处理不同的头部header

    下面将深入探讨Magento如何处理不同页面的头部Header,以及这一过程背后的逻辑和技术细节。 ### Magento处理不同头部Header的核心概念 #### 1. **布局(Layout)系统** Magento的布局系统是其核心特性之一,它允许...

    magento二次开发大全

    1. **MVC架构**:Magento基于Model-View-Controller(MVC)设计模式,这有助于将业务逻辑、数据处理和用户界面分离开来,提高代码的可维护性和可扩展性。`mvc流程.png`可能详细解释了模型、视图和控制器在Magento中...

    Magento 1.3: PHP Developer's Guide ([Packt]出品 Magento 1.3开发手册)

    - **数据模型**:讲解了Magento的数据存储方式,包括表结构设计、关联关系处理等。 - **事件驱动机制**:解释了Magento如何通过事件观察者模式来实现插件和扩展的功能。 **3. 常用模块开发** - **支付模块**:介绍...

    magento-java-master.zip_magento

    4. **Magento API资源**:Magento提供了丰富的API资源,包括顾客管理、订单处理、产品信息、库存管理等。你需要熟悉每个资源的端点、方法(GET、POST、PUT、DELETE)以及它们所需的参数。 5. **异常处理**:在与...

    magento2 developers cookbook

    根据给定文件信息,以下为《Magento 2 Developer's Cookbook》一书中的知识点介绍。 首先,《Magento 2 Developer's Cookbook》是一本针对Magento 2开发的指导手册,它向开发者提供了实用的食谱来解决在Magento 2...

    Magento: Beginner's Guide ([Packt]出品 Magento新手指南)

    ### Magento新手指南知识点详解 #### 一、Magento简介与特性 **Magento**是一款功能强大的开源电子商务平台,由Varien公司(后被Adobe收购)于2008年首次发布。它以其灵活性、可扩展性和丰富的功能集而闻名,是...

    Magento 1.3 PHP Developer's Guide

    - **配置优化**:为了提高性能,可以对缓存机制、图片处理等方面进行优化。 #### 四、主题与模板 - **主题概述**:Magento支持自定义主题,开发者可以通过修改主题文件来改变网站外观。 - **布局文件**:使用XML...

    magento后台显示订单图片

    ### Magento后台显示订单图片知识点详解 #### 一、概述 在电子商务系统中,为了提高工作效率以及方便管理人员查看订单详情,通常需要在后台管理系统中展示与订单相关的商品图片。Magento是一款非常流行的开源电子...

    2016最新 magento 颜色属性切换图片

    "2016最新 Magento 颜色属性切换图片"这个主题就涉及到了Magento系统中的这一关键特性。 首先,我们要理解Magento的颜色属性系统。在Magento中,每个产品都可以有多个属性,其中包括颜色。颜色属性可以是文本类型,...

    magento颜色属性图片展示插件

    "magento颜色属性图片展示插件"就是针对这种情况设计的,它能帮助商家更直观地展示商品的不同颜色选项,提升用户的购物体验。 这款插件适用于Magento 1.7版本,这意味着它已经过兼容性测试,可以稳定地在该版本的...

    magento数据结构分析

    综上所述,Magento的数据结构复杂而精细,涵盖了从商品管理、客户关系维护、订单处理到系统配置和安全控制的各个方面,是实现高效、安全、个性化电子商务服务的基础。理解和掌握Magento的数据结构对于开发人员、系统...

    magento入门学习资料

    Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性著称。作为一款基于PHP开发的系统,它为商家提供了丰富的功能,包括商品管理、订单处理、客户管理、营销工具等。以下将详细介绍`magento入门学习资料...

    Magento-SMTP-Email

    8. **错误处理**:当邮件发送失败时,插件会尝试重新发送,或者将失败的邮件存入队列供后续处理。 9. **兼容性**:与Magento的不同版本兼容,包括社区版和企业版。 安装和配置Magento SMTP Pro Email Extension...

    magento批量上传多图、添加自定义属性1.4、1.5+

    5. **错误处理**:Magento在导入过程中会记录错误,及时查看并解决错误信息,以便顺利完成导入。 6. **性能优化**:对于大规模的商品库,可能需要考虑服务器性能,合理安排导入时间,避免在高峰期影响网站性能。 ...

Global site tag (gtag.js) - Google Analytics