Type
magento2-module
Intervention Image helper for Magento 2
magento2-module
MIT
None
None
None
None
None
A useful template helper for applying transformations to images in Magento 2 using the Intervention Image library.
composer require "dakzilla/intervention-image-helper:~1.0"
php bin/magento setup:upgrade
Call the image helper from any (.phtml) template using this code:
<?php
/** @var \Dakzilla\Intervention\Helper\Image $imageHelper */
$imageHelper = $this->getImageHelper();
?>
You can now call the make
method to load an image URL...
$image = $imageHelper->make('http://mysite.com/media/wysiwyg/home/home-main.jpg')
# Will resolve to /path/to/your/site/pub/media/wysiwyg/home/home-main.jpg
...or a relative image path
$image = $imageHelper->make('test/Pineapple.jpg')
# Will resolve to /path/to/your/site/pub/media/test/Pineapple.jpg
Then, you can chain the desired transformation methods, and finally call the get
method to get the http link to the cached image:
$imageUrl = $image->flip()
->invert()
->resize(350, null, function ($constraint) {
$constraint->aspectRatio();
})
->get();
# Will return http://mysite.com/media/cache/dakzilla_intervention/<cache key>/myimage.jpg
Or, you can do all of these at once with a short, chained syntax:
<img src="<?php echo $imageHelper
->make('test/Pineapple.jpg')
->flip()
->invert()
->resize(350, null, function ($constraint) {
$constraint->aspectRatio();
})
->get(); ?>">
The above example will create a horizontally-flipped, color-inverted and automatically resized version of the original picture that retains the correct ratio. Of course, a more common usage would be to simply crop and/or resize a picture without applying cosmetic filters.
Once set as shown below, image compression and quality will be applied to every image that is generated with this module. This means that if you are generating multiple images on a single page, they will all share the compression settings.
From the template, call these methods on the image helper to enable JPEG compression and set the quality (1-100, lowest to highest):
<?php
/** @var \Dakzilla\Intervention\Helper\Image $imageHelper */
$imageHelper->setCompressImages()->setQuality(75);
?>
If you're okay with the default quality level, you only need to call the setCompressImages
method to enable compression:
<?php
/** @var \Dakzilla\Intervention\Helper\Image $imageHelper */
$imageHelper->setCompressImages();
?>
Delete the image cache directory. By default, this is <magento root>/pub/media/cache/dakzilla_intervention
To avoid potential permission issues, do not re-create this folder after deleting it. Let Magento re-create it automatically.
The helper provides IDE-compatible method hints for every available transformation method. For the most part, these methods are self-explanatory. For further information on these methods, please refer to the Intervention Image documentation.
This module has been tested with Magento 2.1.5. As Magento 2 is still evolving rapidly, there is no guarantee that it will work with every version. However, as this package is unassuming in what it achieves and respects Magento 2 best coding practices, I see no reason why it should cause issues in your installation.
This simple package is piggybacking on the incredible work of Oliver Vogel with his Intervention Image and Image Cache packages.
This module was also inspired by the awesome work of Stämpfli AG and their Magento 2 Image Resizer module.
This module is licensed under the MIT License
Copyright 2017 Simon Dakin
Made with ♥ in Montreal