Here's a code snippet that demonstrates the basic approach:
This also can be view_model and will help you increase your SEO , Mobile page speed
In this code, we create a custom helper class Vendor\Module\Helper\Image that takes in the image path, width, and height as arguments to the resize() method. We first get the absolute path to the image file using the Magento Filesystem class and check if the file exists.
We then create a new image adapter using the AdapterFactory class and open the image file. We resize the image to the specified width and height using the resize() method of the adapter.
Next, we generate a new image file path with the specified width and height and save the resized image using the save() method of the adapter. Finally, we return the URL of the resized image using the Magento UrlInterface class.
You can use this helper class to resize images other than just product images by passing in the image path, width, and height as arguments to the resize() method.
ATTENTION DON't USE OBJECTMANAGER , this is just for example
Comments