IMAGES RE-SIZE IN MAGENTO

简介:

In this article I will show how to use re-size parameters of default Magento images re-size feature.

By default the re-size of the product images working in the following way:

1
2
<?php echo $this ->helper( 'catalog/image' )
             ->init( $_product , 'small_image' )->resize(135); ?>

The code generate the 135х135px image. There are a number of additional parameters that control the image output, here is a list of these parameters with default values:

1
2
3
4
5
6
7
8
9
<?php
     echo  $this ->helper( 'catalog/image' )->init( $_product , 'small_image' )
             ->constrainOnly(false)
             ->keepAspectRatio(true)
             ->keepFrame(true)
             ->keepTransparency(true)
             ->backgroundColor( array (255,255,255))
             ->resize(135, 135);
?>

1 - "Small image" parameter.

There are three types of product images in Magento:

  • Thumbnail
  • Small Image
  • Base Image

Each type have its own image, it allows to load different images for small thumbnail or for big image.

2 - "constrainOnly" parameter.

If the "constrainOnly" parameter is set to true, in this case the images which are smaller than specified value will be not enlarged by Magento. Only border of such images will increase. This is useful if you have small product images and you don't like when Magento pixelate them. This option will not effect images which are bigger than specified value. Example:

3 - "keepAspectRatio" parameter.

If the "keepAspectRatio" parameter is set to true, in this case the proportions of the image will not be modified. Example:

4 - "keepFrame" parameter.

The "keepFrame" parameter guarantees that the image will be not cropped. When "keepAspectRatio" is false the "keepFrame" will not work. Example:

5 - "keepTransparency" parameter.

The "keepTransparency" parameter keep the transparent background of the images. If the "keepTransparency" parameter is set to false, in this case such images will have white background (by default). You can set any color for the background using the backgroundColor parameter. Example:

6 - "backgroundColor" parameter.

The "backgroundColor" allows to set any color as image background. You can enter a color as a RGB code, example: backgroundColor(array(255,255,255)). If the "keepTransparency" parameter is set to true, in this case the background will be not applied to the images with transparency. Example:

7 - "resize" parameter.

Using the "resize" parameter you can set a fixed width and height size for the image. If only one size value is entered and "keepFrame" parameter is set to true, in this case the image height will be equal to the image width.

Examples of the parameters.

1) Fixed height, the width will be calculated automatically:

1
2
3
4
->constrainOnly(true)
->keepAspectRatio(true)
->keepFrame(false)
->resize(null, 135);

Please note that the "keepFrame" parameter is set to false, otherwise all images will be 135х135px.

2) Fixed width, the height will be calculated automatically:

1
2
3
4
->constrainOnly(true)
->keepAspectRatio(true)
->keepFrame(false)
->resize(135, null);

Other useful methods.

The following methods also could be useful:

1
2
3
4
5
6
<?php
     echo $this ->helper( 'catalog/image' )
         ->init( $_product , 'small_image' )->getOriginalWidth();
     echo $this ->helper( 'catalog/image' )
         ->init( $_product , 'small_image' )->getOriginalHeight();
?>

You can get the width and height of the original image, in case if you will need to do some extra calculations or re-size the image in some different way.

原文:http://astrio.net/blog/images-resize-in-magento/


目录
相关文章
|
6月前
|
人工智能 自然语言处理 小程序
创建你的第一个小程序:使用阿里云万小智AI建站一句话生成小程序(图文教程)
阿里云万小智2.0支持自然语言对话生成微信小程序:输入需求描述→自动生成PRD→构建应用→预览编辑→绑定账号→发布上线,全程零代码,快速交付。阿里云万小智官网:https://t.aliyun.com/U/FmBHHe
975 0
|
6月前
|
人工智能 监控 安全
AI Agent进入生产环境前,企业安全必须回答的七个问题
本文以闲鱼AI误将用户照片自动上架事件为引,剖析Agent系统与传统LLM的本质风险差异,指出其“操作半径×不可逆程度”的放大效应。结合《2026国际AI安全报告》及多项前沿研究,提出覆盖权限控制、强制确认、运行监控、安全评估与紧急关停的五层企业级治理框架,强调Agent安全是需持续运营的工程能力,而非一次性技术问题。
507 0
|
8月前
|
JSON 安全 Java
【SpringSecurity新手村系列】(6)基于角色的权限控制、权限拦截注解与自定义无权限页面
本章在 RBAC 角色体系上,开启 @EnableMethodSecurity,使用 @PreAuthorize 配合 hasRole / hasAnyRole 实现 Controller 方法级拦截,并配置自定义 403 无权限页面,重点拆解 ROLE_ 前缀重复拼接的常见坑位。
560 1
|
6月前
|
人工智能 NoSQL 关系型数据库
【MySQL百日打怪升级第22天】事务死锁的成因与避免 —— 死锁检测、锁等待超时
【MySQL死锁精讲】第22天干货:剖析死锁成因(循环加锁)、检测机制(等待图算法)、四大场景(转账/外键/Gap Lock等)及实战避坑方案——统一加锁顺序、缩短事务、合理选隔离级别,附AI诊断工具箱。
482 0
|
数据采集 供应链 API
淘宝 vs 京东电商 API 接口,谁才是数据王者?
在数字化电商时代,API成为企业获取数据的关键工具。本文从功能覆盖、数据质量、开发友好度及商业模型等维度,客观对比淘宝与京东API的优劣,分析两者在不同场景下的适用性,助您判断谁才是真正的“数据王者”。
|
存储 机器学习/深度学习 人工智能
软考中级软件设计师专项-数据结构与算法上篇
软件设计师考试数据结构模块涵盖数组、链表、栈、队列、树、图等基础结构及其操作,重点考查二分查找、快排与归并排序、树/图的DFS/BFS遍历算法,要求掌握时间与空间复杂度分析,理解哈希、堆的应用场景,强调通过合理选择数据结构优化程序性能,解决存储管理与计算效率问题,为系统设计奠定核心逻辑基础。
1243 1
软考中级软件设计师专项-数据结构与算法上篇
|
算法 搜索推荐 Java
贪心算法:部分背包问题深度解析
该Java代码基于贪心算法求解分数背包问题,通过按单位价值降序排序,优先装入高价值物品,并支持部分装入。核心包括冒泡排序优化、分阶段装入策略及精度控制,体现贪心选择性质,适用于可分割资源的最优化场景。
743 1
贪心算法:部分背包问题深度解析
|
域名解析 网络协议 安全
|
机器学习/深度学习 人工智能 监控
AI 视频监控技术核心解析:三大底层能力支撑智能化升级
AI视频监控突破传统安防局限,依托三大核心技术:从“被动感知”到“主动理解”,实现精准场景识别;从“孤立运行”到“深度协同”,构建业务联动闭环;从“高门槛应用”到“普惠化落地”,降低部署成本与使用门槛。技术融合场景定制、智能决策与轻量化架构,推动安防向高效、智能、普及化方向升级。
1799 0
|
存储 安全 Java
shiro学习二:shiro的加密认证详解,加盐与不加盐两个版本。
这篇文章详细介绍了Apache Shiro安全框架中密码的加密认证机制,包括不加盐和加盐两种加密方式的实现和测试。
1147 0