How to Create an image resizer in Magento 2

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

/**
 * Copyright © https://armmage.com/ All rights reserved.
 * See COPYING.txt for license details.
 */

 declare(strict_types=1);

namespace Vendor\Module\Model;

use Magento\Framework\Image\AdapterFactory;
use Magento\Framework\Filesystem;
use Magento\Framework\UrlInterface;

class Image
{  
    /**
     * @var Filesystem
     */
    private $filesystem;

    /**
     * @var UrlInterface
     */
    private $urlBuilder;

    /**
     * @var AdapterFactory
     */
    private $adapterFactory;

    /**
     * @param Filesystem $filesystem,
     * @param UrlInterface $urlBuilder,
     * @param AdapterFactory $adapterFactory
     */
    public function __construct(
        Filesystem $filesystem,
        UrlInterface $urlBuilder,
        AdapterFactory $adapterFactory
    ) {
        $this->filesystem = $filesystem;
        $this->urlBuilder = $urlBuilder;
        $this->adapterFactory = $adapterFactory;
    }

    public function resize($imagePath, $width = null, $height = null)
    {
        $absolutePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)
->getAbsolutePath($imagePath);
        if (!file_exists($absolutePath)) {
            return $this->urlBuilder->getUrl('images/placeholder.png');
        }
        $imageAdapter = $this->adapterFactory->create();
        $imageAdapter->open($absolutePath);
        $imageAdapter->resize($width, $height);
        $resizedImagePath = str_replace('.jpg', "_{$width}x{$height}.jpg", $imagePath);
        $resizedImageAbsolutePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)
->getAbsolutePath($resizedImagePath);
        $imageAdapter->save($resizedImageAbsolutePath);
        return $this->urlBuilder
->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]) . $resizedImagePath;
    }
}

 

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

Ready to elevate your e-commerce business?

Discuss your business objectives with us. Get in touch today to explore ways we can assist in reaching them.

Copyright © 2021-2024 ArmMage LLC. All rights reserved.