How to resize custom image in magento 2?

We can resize the custom image in Magento 2 with a simple coding snippet.

Many times we need to resize images in the Magento project for a custom module requirement or category images.

For Image resizing, we need to create the Block file under the module and called the function under the template file.

We have created a custom getResizeImage() function under the block file and passed the first parameter as the actual image name only, the second parameter will be the width and the third parameter is the height.

For Product image resize refer blog, Product image resize

<?php
namespace Rbj\ImageResize\Block\;

use Magento\Framework\View\Element\Template;
use Magento\Framework\App\Filesystem\DirectoryList;

class ImageResize extends Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Image\AdapterFactory $imageFactory,
        \Magento\Framework\Filesystem $filesystem,
        array $data = []
    ) {
        $this->storeManager = $context->getStoreManager();
        $this->imageFactory = $imageFactory;
        $this->_filesystem = $filesystem;
        $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
        parent::__construct(
            $context,
            $data
        );
    }

    /**
     * @param $imageName string imagename only(abc.jpg)
     * @param $width int
     * @param $height int
     * @return string
     */
    public function getResizeImage($imageName,$width = 258,$height = 200)
    {
        /* Real path of image from directory */
        $realPath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('catalog/category/'.$imageName);
        if (!$this->_directory->isFile($realPath) || !$this->_directory->isExist($realPath)) {
            return false;
        }
        /* Target directory path where our resized image will be save */
        $targetDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'x'.$height);
        $pathTargetDir = $this->_directory->getRelativePath($targetDir);
        /* If Directory not available, create it */
        if (!$this->_directory->isExist($pathTargetDir)) {
            $this->_directory->create($pathTargetDir);
        }
        if (!$this->_directory->isExist($pathTargetDir)) {
            return false;
        }

        $image = $this->imageFactory->create();
        $image->open($realPath);
        $image->keepAspectRatio(true);
        $image->resize($width,$height);
        $dest = $targetDir . '/' . pathinfo($realPath, PATHINFO_BASENAME);
        $image->save($dest);
        if ($this->_directory->isFile($this->_directory->getRelativePath($dest))) {
            return $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'x'.$height.'/'.$imageName;
        }
        return false;
    }
}

Called from Template file,

<?php
    $imageResize = $block->getResizeImage($category->getImage(),150,150);
?>
<img src="<?= $imageResize; ?>" alt="<?= __('Image name'); ?>" />

You got the output as a 150×150 Resize the image.