web200/magento-mod-web200_image-resize
Composer 安装命令:
composer require web200/magento-mod-web200_image-resize
包简介
Magento 2 Module to add simple image resizing
README 文档
README
Magento 2 Module to add simple image resizing capabilities in all blocks and .phtml templates
Installation
$ composer require "web200/magento-mod-web200_image-resize":"*"
Simple resize image usage
ViewModel
Layout
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="header.container"> <block name="authlinks" template="Magento_Theme::authlinks.phtml"> <arguments> <argument name="image_resize" xsi:type="object">Web200\ImageResize\ViewModel\ImageResize</argument> </arguments> </block> </referenceContainer> </body> </page>
phtml
<?php /** @var \Web200\ImageResize\ViewModel\ImageResize $imageResize */ ?> <?php $imageResize = $block->getImageResize() ?> <?php /** * $originalImage can be a full url image : https://mywebsite.com/pub/media/catalog/product/a/b/001.jpg * or relative media path : catalog/product/a/b/001.jpg */ ?> <?php $imageResize->getResize()->resizeAndGetUrl($originalImage, $width, $height, $resizeSettings);
Helper
phtml
<?php /** @var \Web200\ImageResize\Helper\ImageResize $resizeHelper */ ?> <?php $resizeHelper = $this->helper(\Web200\ImageResize\Helper\ImageResize::class) ?> <?php /** * $originalImage can be a full url image : https://mywebsite.com/pub/media/catalog/product/a/b/001.jpg * or relative media path : catalog/product/a/b/001.jpg */ ?> <?php $resizeHelper->getResize()->resizeAndGetUrl($originalImage, $width, $height, $resizeSettings);
Advanced resize and display image usage
- Display alternative image size with breakpoint / retina settings.
- Option to display webp images, available in Store > Configuration > Image Resize.
- In order to display images use a js library : https://github.com/verlok/vanilla-lazyload
ViewModel
Layout
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="header.container"> <block name="authlinks" template="Magento_Theme::authlinks.phtml"> <arguments> <argument name="image_display" xsi:type="object">Web200\ImageResize\ViewModel\ImageDisplay</argument> </arguments> </block> </referenceContainer> </body> </page>
phtml
<?php /** @var \Web200\ImageResize\ViewModel\ImageDisplay $imageDisplay */ ?> <?php $imageDisplay = $block->getImageDisplay() ?> <?php /** * $originalImage can be a full url image : https://mywebsite.com/pub/media/catalog/product/a/b/001.jpg * or relative media path : catalog/product/a/b/001.jpg */ ?> <?php $imageDisplay->getDisplay()->getImage( 'catalog/product/a/b/001.jpg', 250, 250, [ 'title' => $block->stripTags('Some Label'), 'retina' => true, 'breakpoints' => ['1440' => ['325', '325'], '768' => ['250', '250'], '0' => ['150', '150']] ] );
Display in html
<picture> <source media="(min-width: 768px)" data-srcset="https://domain.com/media/web200_imageresize/cache/cms/images/97x97_co_ar_tr_fr_bc_85/empty.webp 1x, https://domain.com/media/web200_imageresize/cache/cms/images/194x194_co_ar_tr_fr_bc_85/empty.webp 2x" /> <source media="(min-width: 0px)" data-srcset="https://domain.com/media/web200_imageresize/cache/cms/images/44x44_co_ar_tr_fr_bc_85/empty.webp 1x, https://domain.com/media/web200_imageresize/cache/cms/images/88x88_co_ar_tr_fr_bc_85/empty.webp 2x" /> <img alt="Service 1" title="Service 1" class="lazy" src="https://domain.com/media/web200_imageresize/cache/catalog/product/placeholder/default/97x97_co_ar_tr_fr_bc_85/placeholder.jpg" data-src="https://domain.com/media/web200_imageresize/cache/cms/images/97x97_co_ar_tr_fr_bc_85/empty.jpg" data-srcset="https://domain.com/media/web200_imageresize/cache/cms/images/97x97_co_ar_tr_fr_bc_85/empty.jpg 1x, https://domain.com/media/web200_imageresize/cache/cms/images/194x194_co_ar_tr_fr_bc_85/empty.webp 2x"/> </picture>
Resize Settings
The folowing is a list of the resize settings that can be set directory to $resizeSettings parameter
| Name | Default | Type |
|---|---|---|
| constrainOnly | true | Boolean |
| keepAspectRatio | true | Boolean |
| keepTransparency | true | Boolean |
| keepFrame | false | Boolean |
| backgroundColor | null | Array with RGB values ([255,255,255]) |
| quality | 85 | Number 1-100 |
| --- | --- | --- |
| watermark | null | array |
| watermark['imagepath'] | null | string |
| watermark['x'] | null | int |
| watermark['y'] | null | int |
| watermark['opacity'] | null | string |
| watermark['title'] | null | string |
or configurate in Store > Configuration > Image Resize
SVG
Authorize and sanitize SVG image upload. Disabled by default. You need to enable it in Store > Configuration > Image Resize.
- Add svg upload for icon and logo website
- Add svg upload for backend configuration files
- Add svg upload for product images
Cache
Resized images are saved in cache to improve performance. That way, if an image was already resized, we just use the one in cache.
If you need to, you can clear the resized images cache on the Admin Cache Management
Prerequisites
- PHP >= 7.1.*
- Magento >= 2.3.*
Forked from
web200/magento-mod-web200_image-resize 适用场景与选型建议
web200/magento-mod-web200_image-resize 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33.98k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2019 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 web200/magento-mod-web200_image-resize 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 web200/magento-mod-web200_image-resize 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 33.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: OSL-3.0
- 更新时间: 2019-10-15

