Magento 2 Module to add simple image resizing capabilities in all blocks and .phtml templates
This fork continues the development of the original module by Staempfli, which has not been maintained for several years. The primary goal of this fork is to ensure compatibility with PHP 8.2 and Magento 2.4.6, reflecting the latest requirements and standards. Feel free to use and contribute to the enhancements made here.
$ composer require "staempfli/magento2-module-image-resizer":"~2.0"
imageResizer
is automatically available in all frontend Blocks.
You can resize your images just calling a method:
/** @var \Staempfli\ImageResizer\Model\Resizer $imageResizer */
$imageResizer = $block->getImageResizer();
$resizedImageUrl = $imageResizer->resizeAndGetUrl(<originalImageUrl>, $width, $height, [$resizeSettings]);
You can do that directly on the .phtml or in your custom Block.
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
- PHP >= 8.2.*
- Magento >= 2.4.6
The folowing is a list of the resize settings that can be set:
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 |