How to resize custom image in magento 2?

We can resize the custom image in Magento 2 by simple coding snippet. Many times we need to resize images in Magento 2 for a custom module or like category images.

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

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

For Product image resize refer blog, Product image resize

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);

     * @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)) {
        if (!$this->_directory->isExist($pathTargetDir)) {
            return false;

        $image = $this->imageFactory->create();
        $dest = $targetDir . '/' . pathinfo($realPath, PATHINFO_BASENAME);
        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,

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

You got the output as 150×150 Resize image.