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 folderif(!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 directoryif (!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图片处理 Customize Magento’s Image Resize Functionality相关推荐

  1. Magento教程 18:Magento功能导览(2) 展示商品

    Magento是强大的电子商务网站,可以多样化的呈现商品以及支援多种客制化选项,提供客户在选购商品时可以附上更加详细的资讯,以下就来看看Magento在商品方面的呈现. 分类商品页面 Magento教 ...

  2. Magento教程 12:Magento快速上传大量商品的方法

    利用.CSV档上传大量商品是最佳选择 在Magento上传大量商品到时,经常会遇到的几个问题,非常耗时间.出现错误.有部分商品是缺货状态. 当你有上百个不同属性的商品,而且只有部分商品是现货供应的情况 ...

  3. Magento教程 17:Magento功能导览(1) 会员功能

    Magento系统内建的会员系统功能齐全,可以满足大部分电子商务网站需求,下面将介绍Magento的会员系统. 登入页面 Magento教程 17:Magento功能导览(1) 会员功能 在使用任何有 ...

  4. Magento教程 16:Magento价格规则的优先度

    做为一个刚接触Magento的菜鸟,相信在设定商品时一定会感到一头雾水,这么多的参数,光是设定价格就被搞到一个头两个大了,到底该怎么弄呢? 价格.优惠价.群组价.阶层价都设定的话,究竟是会采用哪一个呢 ...

  5. Magento教程 15:Magento 批次修改产品资料

    当你拥有多笔商品资料需要修改的时候,一个一个进入产品编辑页面修改是非常花费时间的,Magento后台的产品列表中提供了批次修改的功能,减轻你在编辑大量商品资料的负担,今天要告诉大家如何在Magento ...

  6. Magento教程 2:Magento 社群版安装教学!

    想要架设Magento网站经营电子商务,却不知道如何着手吗? Astral Web将会为您介绍如何安装Magento Community Edition 1.7.0.2 社群版,帮您建立网站第一步! ...

  7. html商品图片放大插件,PS图片无损放大/图像处理插件-ON1 Resize 2018.5

    PS图片无损放大/图像处理插件-ON1 Resize 2018.5 书法字体2019.03.30ON1 Resize ON1 Resize 2018是专门为摄影师打造的一款功能强大的照片后期处理软件, ...

  8. 深度linux magento,linux下安装magento

    一.环境: 1.windows 2008 :hyper-v 2.centos 6.5-i386 (64位hyper-v不识别) 3.Magento1.8.1.0 社区版(CE) 二.开始: 1.安装c ...

  9. python opencv图片放大 缩小_Python OpenCV之图片缩放的实现(cv2.resize)

    OpenCV函数原型: cv2.resize(InputArray src, OutputArray dst, Size, fx, fy, interpolation) 参数解释: InputArra ...

最新文章

  1. Accuracy和Precision
  2. 使用VMware VSphere WebService SDK进行开发 (二)——获取虚拟机cpu的使用情况
  3. Java中内存中的Heap、Stack与程序运行的关系
  4. LeetCode 1409. 查询带键的排列(map模拟)
  5. 解决layui隐藏域:不显示问题(含案例、代码、截图)
  6. Linux——vi编辑器
  7. 构建虚拟工控环境系列 - 罗克韦尔虚拟PLC
  8. easyui treegrid 获取新添加行inserted_IDEA 2020.2 稳定版发布,带来了不少新功能...
  9. 超清晰的 DNS 原理入门指南 (资源)
  10. [转载]HTTP的幂等性
  11. win10桌面背景为什么突然变黑了 win10桌面背景不显示解决方法
  12. 天下武功唯快不破 实验吧(小结)
  13. lof基金溢价率php源码,一文读懂LOF基金套利策略 LOF是球友们很喜欢的一类基金,这类基金不仅可以在场内进行高效买卖,而且还可以进行折溢价套利。不过这里面牵扯到很多细节,... - 雪球...
  14. 《画解数据结构》三十张彩图,画解二叉搜索树
  15. 工商银行携手数美科技,赋能数字化业务高质量发展
  16. 银行手机APP安全评估报告【转载】
  17. BT种子的技术原理是什么?就是.torrent文件该如何理解?
  18. 用CNN做在NLP句子分类
  19. dell 720 raid 配置
  20. 让海航买不起的当当究竟值多少钱?90亿够不够?

热门文章

  1. 猜数字(HDU-2178)
  2. 信息学奥赛C++语言:最大数max(x,y,z)
  3. 信息学奥赛C++语言:最高分数的学生姓名
  4. 信息学奥赛C++语言:求和4
  5. 57 SD配置-科目分配-定义客户账户分配组
  6. uiwebview 修改html,修改UIWebView加载的html文本属性
  7. OpenCV/Python:相机标定
  8. mysql触发器如何获取当前表名_Mysql如何获取中位数
  9. win10缺少 `VCRUNTIME140.dll` 文件(解决篇)
  10. ssm项目直接加html文件,如何把ssm项目和vue项目部署在云服务器(上)