How to resize Image in Magento 2?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I want to resized image of subcategory. for that,
I added this code in .phtml file.I'm getting wrong path for image



<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$catId = 3; //Parent Category ID
$subCategory = $objectManager->create('MagentoCatalogModelCategory')->load($catId);
$subCats = $subCategory->getChildrenCategories();
$_helper = $this->helper('MagentoCatalogHelperOutput');
$_imagehelper = $this->helper('MagentoCatalogHelperImage');
$count = 0;
?>
<div class="container homecat">
<div class="row">
<?php
foreach ($subCats as $subcat) {
$_category = $objectManager->create('MagentoCatalogModelCategory')->load($subcat->getId());
$subcaturl = $subcat->getUrl();
$_imgHtml = '';

$subcaturl = $_imagehelper->init($_category, 'image')->keepAspectRatio(TRUE)
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepTransparency(TRUE)
->resize('256','128')
->getUrl();
echo $subcaturl ."<br/>";
$_imgHtml = '<img src="' . $subcaturl . '" />';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
echo $_imgHtml;
} ?>
</div>
</div>


I am getting




http://Hostname/als2/pub/static/version1549014696/frontend/Theme/Name/en_US/Magento_Catalog/images/product/placeholder/.jpg











share|improve this question

























  • Bhakti, Please check my updated ans in detailed way.

    – Aasim Goriya
    Feb 1 at 12:34













  • thanks, I found the solution @AasimGoriya

    – bhakti thakkar
    Feb 2 at 4:53


















1















I want to resized image of subcategory. for that,
I added this code in .phtml file.I'm getting wrong path for image



<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$catId = 3; //Parent Category ID
$subCategory = $objectManager->create('MagentoCatalogModelCategory')->load($catId);
$subCats = $subCategory->getChildrenCategories();
$_helper = $this->helper('MagentoCatalogHelperOutput');
$_imagehelper = $this->helper('MagentoCatalogHelperImage');
$count = 0;
?>
<div class="container homecat">
<div class="row">
<?php
foreach ($subCats as $subcat) {
$_category = $objectManager->create('MagentoCatalogModelCategory')->load($subcat->getId());
$subcaturl = $subcat->getUrl();
$_imgHtml = '';

$subcaturl = $_imagehelper->init($_category, 'image')->keepAspectRatio(TRUE)
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepTransparency(TRUE)
->resize('256','128')
->getUrl();
echo $subcaturl ."<br/>";
$_imgHtml = '<img src="' . $subcaturl . '" />';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
echo $_imgHtml;
} ?>
</div>
</div>


I am getting




http://Hostname/als2/pub/static/version1549014696/frontend/Theme/Name/en_US/Magento_Catalog/images/product/placeholder/.jpg











share|improve this question

























  • Bhakti, Please check my updated ans in detailed way.

    – Aasim Goriya
    Feb 1 at 12:34













  • thanks, I found the solution @AasimGoriya

    – bhakti thakkar
    Feb 2 at 4:53














1












1








1








I want to resized image of subcategory. for that,
I added this code in .phtml file.I'm getting wrong path for image



<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$catId = 3; //Parent Category ID
$subCategory = $objectManager->create('MagentoCatalogModelCategory')->load($catId);
$subCats = $subCategory->getChildrenCategories();
$_helper = $this->helper('MagentoCatalogHelperOutput');
$_imagehelper = $this->helper('MagentoCatalogHelperImage');
$count = 0;
?>
<div class="container homecat">
<div class="row">
<?php
foreach ($subCats as $subcat) {
$_category = $objectManager->create('MagentoCatalogModelCategory')->load($subcat->getId());
$subcaturl = $subcat->getUrl();
$_imgHtml = '';

$subcaturl = $_imagehelper->init($_category, 'image')->keepAspectRatio(TRUE)
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepTransparency(TRUE)
->resize('256','128')
->getUrl();
echo $subcaturl ."<br/>";
$_imgHtml = '<img src="' . $subcaturl . '" />';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
echo $_imgHtml;
} ?>
</div>
</div>


I am getting




http://Hostname/als2/pub/static/version1549014696/frontend/Theme/Name/en_US/Magento_Catalog/images/product/placeholder/.jpg











share|improve this question
















I want to resized image of subcategory. for that,
I added this code in .phtml file.I'm getting wrong path for image



<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$catId = 3; //Parent Category ID
$subCategory = $objectManager->create('MagentoCatalogModelCategory')->load($catId);
$subCats = $subCategory->getChildrenCategories();
$_helper = $this->helper('MagentoCatalogHelperOutput');
$_imagehelper = $this->helper('MagentoCatalogHelperImage');
$count = 0;
?>
<div class="container homecat">
<div class="row">
<?php
foreach ($subCats as $subcat) {
$_category = $objectManager->create('MagentoCatalogModelCategory')->load($subcat->getId());
$subcaturl = $subcat->getUrl();
$_imgHtml = '';

$subcaturl = $_imagehelper->init($_category, 'image')->keepAspectRatio(TRUE)
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepTransparency(TRUE)
->resize('256','128')
->getUrl();
echo $subcaturl ."<br/>";
$_imgHtml = '<img src="' . $subcaturl . '" />';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
echo $_imgHtml;
} ?>
</div>
</div>


I am getting




http://Hostname/als2/pub/static/version1549014696/frontend/Theme/Name/en_US/Magento_Catalog/images/product/placeholder/.jpg








magento2 image image-resizing sub-categories






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 4 at 7:46









Ketan Borada

406938




406938










asked Feb 1 at 9:54









bhakti thakkarbhakti thakkar

564114




564114













  • Bhakti, Please check my updated ans in detailed way.

    – Aasim Goriya
    Feb 1 at 12:34













  • thanks, I found the solution @AasimGoriya

    – bhakti thakkar
    Feb 2 at 4:53



















  • Bhakti, Please check my updated ans in detailed way.

    – Aasim Goriya
    Feb 1 at 12:34













  • thanks, I found the solution @AasimGoriya

    – bhakti thakkar
    Feb 2 at 4:53

















Bhakti, Please check my updated ans in detailed way.

– Aasim Goriya
Feb 1 at 12:34







Bhakti, Please check my updated ans in detailed way.

– Aasim Goriya
Feb 1 at 12:34















thanks, I found the solution @AasimGoriya

– bhakti thakkar
Feb 2 at 4:53





thanks, I found the solution @AasimGoriya

– bhakti thakkar
Feb 2 at 4:53










2 Answers
2






active

oldest

votes


















2














Create resize image by passing image URL




create Block file appcodeKetanResizeHelperImage.php



pass url and dimension




<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/




namespace KetanResizeHelper;

use MagentoFrameworkFilesystem;
use MagentoFrameworkUrl;
use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkAppFilesystemDirectoryList;

class Image extends MagentoFrameworkAppHelperAbstractHelper {
protected $scopeConfig;
protected $storeManager;
protected $messageManager;
protected $_response;
protected $_resourceConfig;
protected $_responseFactory;
protected $_url;
protected $_filesystem;
protected $_directory;
protected $_imageFactory;
public function __construct(
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkMessageManagerInterface $messageManager,
MagentoFrameworkAppResponseInterface $response,
MagentoFrameworkAppConfigStorageWriterInterface $resourceConfig,
MagentoFrameworkAppResponseFactory $responseFactory,
MagentoFrameworkUrlInterface $url,
MagentoFrameworkFilesystem $filesystem,
MagentoFrameworkImageAdapterFactory $imageFactory
)
{
$this->scopeConfig = $scopeConfig;
$this->_storeManager=$storeManager;
$this->messageManager=$messageManager;
$this->_response=$response;
$this->_resourceConfig=$resourceConfig;
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_filesystem = $filesystem;
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->_imageFactory = $imageFactory;
}
public function imageResize(
$src,
$width=35,
$height=35,
$dir='resize/'
){
if (!@getimagesize($src)) {
$src = $this->_storeManager->getStore()->getBaseUrl().'pub/media/catalog/product/placeholder/'.$this->scopeConfig->getValue('catalog/placeholder/small_image_placeholder',MagentoStoreModelScopeInterface::SCOPE_STORE);
}
$absPath = $src;
$imageResized = $this->_filesystem
->getDirectoryRead(DirectoryList::MEDIA)
->getAbsolutePath($dir).
$this->getNewDirectoryImage($src);
$imageResize = $this->_imageFactory->create();
$imageResize->open($absPath);
$imageResize->backgroundColor([255, 255, 255]);
$imageResize->constrainOnly(TRUE);
$imageResize->keepTransparency(TRUE);
$imageResize->keepFrame(true);
$imageResize->keepAspectRatio(true);
$imageResize->resize($width,$height);
$dest = $imageResized ;
$imageResize->save($dest);
$resizedURL= $this->_storeManager->getStore()
->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).
$dir.$this->getNewDirectoryImage($src);
return $resizedURL;
}
public function getNewDirectoryImage($src){
$segments = array_reverse(explode('/',$src));
$first_dir = substr($segments[0],0,1);
$second_dir = substr($segments[0],1,1);
return 'cache/'.$first_dir.'/'.$second_dir.'/'.$segments[0];
}
}


In your phtml file



<?php foreach ($subcats as $subcat) { 
$_imgUrl = $subcat->getImageUrl(); ?>

<img src="<?php echo $this->helper('KetanresizeHelperImage')->imageResize($_imgUrl,'256','128','subtcat/'); ?>">

<?php } ?>





share|improve this answer
























  • getting @Error filtering template: Warning: getimagesize(192.168.1.122/als2/pub/media/catalog/product/placeholder): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in D:xampphtdocsals2vendormagentoframeworkImageAdapterAbstractAdapter.php on line 304

    – bhakti thakkar
    Feb 1 at 10:57











  • upload place holder image in admin small_image_placeholder prntscr.com/mf6jlv f there is no image it will take from there

    – Ketan Borada
    Feb 1 at 11:02













  • I got resized img of placeholder but I want subcategory image

    – bhakti thakkar
    Feb 1 at 11:24











  • be sure are you passing category image url in $_imgUrl otherwise it will take place holder image

    – Ketan Borada
    Feb 1 at 11:26



















2














Please try this code for resized any images.



Using $objectManager :



$objectManager = MagentoFrameworkAppObjectManager::getInstance();

$directory = $objectManager->get('MagentoFrameworkFilesystemDirectoryList');
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$fileSystem = $objectManager->get('MagentoFrameworkFilesystem');

$absolutePath = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$subcat->getUrl();
$imageResized = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$subcat->getUrl();

//create image factory...
$imageResize = $objectManager->create('MagentoFrameworkImageAdapterFactory');
$imageResize->open($absolutePath);
$imageResize->constrainOnly(TRUE);
$imageResize->keepTransparency(TRUE);
$imageResize->keepFrame(FALSE);
$imageResize->keepAspectRatio(TRUE);
$imageResize->resize($width,$height);
//destination folder
$destination = $imageResized;
//save image
$imageResize->save($destination);

$resizedURL = $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

echo $resizedURL;


Using Helper :



<?php
namespace VendorModuleHelper;

use MagentoStoreModelStoreManagerInterface;

class Imageresize extends MagentoFrameworkAppHelperAbstractHelper
{
protected $_filesystem ;
protected $_imageFactory;
protected $_storeManager;
protected $_objectManager;

public function __construct(
MagentoFrameworkFilesystem $filesystem,
MagentoFrameworkObjectManagerInterface $objectManager,
StoreManagerInterface $storeManager,
MagentoFrameworkImageAdapterFactory $imageFactory
) {
$this->_filesystem = $filesystem;
$this->_imageFactory = $imageFactory;
$this->_objectManager = $objectManager;
$this->_storeManager = $storeManager;
}

// pass imagename, width and height
public function resize($image, $width = null, $height = null)
{
$absolutePath = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$image;

$imageResized = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image;
//create image factory...
$imageResize = $this->_imageFactory->create();
$imageResize->open($absolutePath);
$imageResize->constrainOnly(TRUE);
$imageResize->keepTransparency(TRUE);
$imageResize->keepFrame(FALSE);
$imageResize->keepAspectRatio(TRUE);
$imageResize->resize($width,$height);
//destination folder
$destination = $imageResized;
//save image
$imageResize->save($destination);

$resizedURL = $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

return $resizedURL;
}
}
?>


And then add following code in your .phtml



<?php $imgResized = $this->helper('VendorModuleHelperImageresize')->resize($subcaturl,277,180); ?>
<a href="<?php echo $br['link']; ?>"><img src="<?php echo $imgResized; ?>" /></a>


Please check and let me know if any issue.






share|improve this answer


























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f260123%2fhow-to-resize-image-in-magento-2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Create resize image by passing image URL




    create Block file appcodeKetanResizeHelperImage.php



    pass url and dimension




    <?php
    /**
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */




    namespace KetanResizeHelper;

    use MagentoFrameworkFilesystem;
    use MagentoFrameworkUrl;
    use MagentoFrameworkAppHelperAbstractHelper;
    use MagentoFrameworkAppFilesystemDirectoryList;

    class Image extends MagentoFrameworkAppHelperAbstractHelper {
    protected $scopeConfig;
    protected $storeManager;
    protected $messageManager;
    protected $_response;
    protected $_resourceConfig;
    protected $_responseFactory;
    protected $_url;
    protected $_filesystem;
    protected $_directory;
    protected $_imageFactory;
    public function __construct(
    MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoFrameworkMessageManagerInterface $messageManager,
    MagentoFrameworkAppResponseInterface $response,
    MagentoFrameworkAppConfigStorageWriterInterface $resourceConfig,
    MagentoFrameworkAppResponseFactory $responseFactory,
    MagentoFrameworkUrlInterface $url,
    MagentoFrameworkFilesystem $filesystem,
    MagentoFrameworkImageAdapterFactory $imageFactory
    )
    {
    $this->scopeConfig = $scopeConfig;
    $this->_storeManager=$storeManager;
    $this->messageManager=$messageManager;
    $this->_response=$response;
    $this->_resourceConfig=$resourceConfig;
    $this->_responseFactory = $responseFactory;
    $this->_url = $url;
    $this->_filesystem = $filesystem;
    $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
    $this->_imageFactory = $imageFactory;
    }
    public function imageResize(
    $src,
    $width=35,
    $height=35,
    $dir='resize/'
    ){
    if (!@getimagesize($src)) {
    $src = $this->_storeManager->getStore()->getBaseUrl().'pub/media/catalog/product/placeholder/'.$this->scopeConfig->getValue('catalog/placeholder/small_image_placeholder',MagentoStoreModelScopeInterface::SCOPE_STORE);
    }
    $absPath = $src;
    $imageResized = $this->_filesystem
    ->getDirectoryRead(DirectoryList::MEDIA)
    ->getAbsolutePath($dir).
    $this->getNewDirectoryImage($src);
    $imageResize = $this->_imageFactory->create();
    $imageResize->open($absPath);
    $imageResize->backgroundColor([255, 255, 255]);
    $imageResize->constrainOnly(TRUE);
    $imageResize->keepTransparency(TRUE);
    $imageResize->keepFrame(true);
    $imageResize->keepAspectRatio(true);
    $imageResize->resize($width,$height);
    $dest = $imageResized ;
    $imageResize->save($dest);
    $resizedURL= $this->_storeManager->getStore()
    ->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).
    $dir.$this->getNewDirectoryImage($src);
    return $resizedURL;
    }
    public function getNewDirectoryImage($src){
    $segments = array_reverse(explode('/',$src));
    $first_dir = substr($segments[0],0,1);
    $second_dir = substr($segments[0],1,1);
    return 'cache/'.$first_dir.'/'.$second_dir.'/'.$segments[0];
    }
    }


    In your phtml file



    <?php foreach ($subcats as $subcat) { 
    $_imgUrl = $subcat->getImageUrl(); ?>

    <img src="<?php echo $this->helper('KetanresizeHelperImage')->imageResize($_imgUrl,'256','128','subtcat/'); ?>">

    <?php } ?>





    share|improve this answer
























    • getting @Error filtering template: Warning: getimagesize(192.168.1.122/als2/pub/media/catalog/product/placeholder): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in D:xampphtdocsals2vendormagentoframeworkImageAdapterAbstractAdapter.php on line 304

      – bhakti thakkar
      Feb 1 at 10:57











    • upload place holder image in admin small_image_placeholder prntscr.com/mf6jlv f there is no image it will take from there

      – Ketan Borada
      Feb 1 at 11:02













    • I got resized img of placeholder but I want subcategory image

      – bhakti thakkar
      Feb 1 at 11:24











    • be sure are you passing category image url in $_imgUrl otherwise it will take place holder image

      – Ketan Borada
      Feb 1 at 11:26
















    2














    Create resize image by passing image URL




    create Block file appcodeKetanResizeHelperImage.php



    pass url and dimension




    <?php
    /**
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */




    namespace KetanResizeHelper;

    use MagentoFrameworkFilesystem;
    use MagentoFrameworkUrl;
    use MagentoFrameworkAppHelperAbstractHelper;
    use MagentoFrameworkAppFilesystemDirectoryList;

    class Image extends MagentoFrameworkAppHelperAbstractHelper {
    protected $scopeConfig;
    protected $storeManager;
    protected $messageManager;
    protected $_response;
    protected $_resourceConfig;
    protected $_responseFactory;
    protected $_url;
    protected $_filesystem;
    protected $_directory;
    protected $_imageFactory;
    public function __construct(
    MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoFrameworkMessageManagerInterface $messageManager,
    MagentoFrameworkAppResponseInterface $response,
    MagentoFrameworkAppConfigStorageWriterInterface $resourceConfig,
    MagentoFrameworkAppResponseFactory $responseFactory,
    MagentoFrameworkUrlInterface $url,
    MagentoFrameworkFilesystem $filesystem,
    MagentoFrameworkImageAdapterFactory $imageFactory
    )
    {
    $this->scopeConfig = $scopeConfig;
    $this->_storeManager=$storeManager;
    $this->messageManager=$messageManager;
    $this->_response=$response;
    $this->_resourceConfig=$resourceConfig;
    $this->_responseFactory = $responseFactory;
    $this->_url = $url;
    $this->_filesystem = $filesystem;
    $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
    $this->_imageFactory = $imageFactory;
    }
    public function imageResize(
    $src,
    $width=35,
    $height=35,
    $dir='resize/'
    ){
    if (!@getimagesize($src)) {
    $src = $this->_storeManager->getStore()->getBaseUrl().'pub/media/catalog/product/placeholder/'.$this->scopeConfig->getValue('catalog/placeholder/small_image_placeholder',MagentoStoreModelScopeInterface::SCOPE_STORE);
    }
    $absPath = $src;
    $imageResized = $this->_filesystem
    ->getDirectoryRead(DirectoryList::MEDIA)
    ->getAbsolutePath($dir).
    $this->getNewDirectoryImage($src);
    $imageResize = $this->_imageFactory->create();
    $imageResize->open($absPath);
    $imageResize->backgroundColor([255, 255, 255]);
    $imageResize->constrainOnly(TRUE);
    $imageResize->keepTransparency(TRUE);
    $imageResize->keepFrame(true);
    $imageResize->keepAspectRatio(true);
    $imageResize->resize($width,$height);
    $dest = $imageResized ;
    $imageResize->save($dest);
    $resizedURL= $this->_storeManager->getStore()
    ->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).
    $dir.$this->getNewDirectoryImage($src);
    return $resizedURL;
    }
    public function getNewDirectoryImage($src){
    $segments = array_reverse(explode('/',$src));
    $first_dir = substr($segments[0],0,1);
    $second_dir = substr($segments[0],1,1);
    return 'cache/'.$first_dir.'/'.$second_dir.'/'.$segments[0];
    }
    }


    In your phtml file



    <?php foreach ($subcats as $subcat) { 
    $_imgUrl = $subcat->getImageUrl(); ?>

    <img src="<?php echo $this->helper('KetanresizeHelperImage')->imageResize($_imgUrl,'256','128','subtcat/'); ?>">

    <?php } ?>





    share|improve this answer
























    • getting @Error filtering template: Warning: getimagesize(192.168.1.122/als2/pub/media/catalog/product/placeholder): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in D:xampphtdocsals2vendormagentoframeworkImageAdapterAbstractAdapter.php on line 304

      – bhakti thakkar
      Feb 1 at 10:57











    • upload place holder image in admin small_image_placeholder prntscr.com/mf6jlv f there is no image it will take from there

      – Ketan Borada
      Feb 1 at 11:02













    • I got resized img of placeholder but I want subcategory image

      – bhakti thakkar
      Feb 1 at 11:24











    • be sure are you passing category image url in $_imgUrl otherwise it will take place holder image

      – Ketan Borada
      Feb 1 at 11:26














    2












    2








    2







    Create resize image by passing image URL




    create Block file appcodeKetanResizeHelperImage.php



    pass url and dimension




    <?php
    /**
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */




    namespace KetanResizeHelper;

    use MagentoFrameworkFilesystem;
    use MagentoFrameworkUrl;
    use MagentoFrameworkAppHelperAbstractHelper;
    use MagentoFrameworkAppFilesystemDirectoryList;

    class Image extends MagentoFrameworkAppHelperAbstractHelper {
    protected $scopeConfig;
    protected $storeManager;
    protected $messageManager;
    protected $_response;
    protected $_resourceConfig;
    protected $_responseFactory;
    protected $_url;
    protected $_filesystem;
    protected $_directory;
    protected $_imageFactory;
    public function __construct(
    MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoFrameworkMessageManagerInterface $messageManager,
    MagentoFrameworkAppResponseInterface $response,
    MagentoFrameworkAppConfigStorageWriterInterface $resourceConfig,
    MagentoFrameworkAppResponseFactory $responseFactory,
    MagentoFrameworkUrlInterface $url,
    MagentoFrameworkFilesystem $filesystem,
    MagentoFrameworkImageAdapterFactory $imageFactory
    )
    {
    $this->scopeConfig = $scopeConfig;
    $this->_storeManager=$storeManager;
    $this->messageManager=$messageManager;
    $this->_response=$response;
    $this->_resourceConfig=$resourceConfig;
    $this->_responseFactory = $responseFactory;
    $this->_url = $url;
    $this->_filesystem = $filesystem;
    $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
    $this->_imageFactory = $imageFactory;
    }
    public function imageResize(
    $src,
    $width=35,
    $height=35,
    $dir='resize/'
    ){
    if (!@getimagesize($src)) {
    $src = $this->_storeManager->getStore()->getBaseUrl().'pub/media/catalog/product/placeholder/'.$this->scopeConfig->getValue('catalog/placeholder/small_image_placeholder',MagentoStoreModelScopeInterface::SCOPE_STORE);
    }
    $absPath = $src;
    $imageResized = $this->_filesystem
    ->getDirectoryRead(DirectoryList::MEDIA)
    ->getAbsolutePath($dir).
    $this->getNewDirectoryImage($src);
    $imageResize = $this->_imageFactory->create();
    $imageResize->open($absPath);
    $imageResize->backgroundColor([255, 255, 255]);
    $imageResize->constrainOnly(TRUE);
    $imageResize->keepTransparency(TRUE);
    $imageResize->keepFrame(true);
    $imageResize->keepAspectRatio(true);
    $imageResize->resize($width,$height);
    $dest = $imageResized ;
    $imageResize->save($dest);
    $resizedURL= $this->_storeManager->getStore()
    ->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).
    $dir.$this->getNewDirectoryImage($src);
    return $resizedURL;
    }
    public function getNewDirectoryImage($src){
    $segments = array_reverse(explode('/',$src));
    $first_dir = substr($segments[0],0,1);
    $second_dir = substr($segments[0],1,1);
    return 'cache/'.$first_dir.'/'.$second_dir.'/'.$segments[0];
    }
    }


    In your phtml file



    <?php foreach ($subcats as $subcat) { 
    $_imgUrl = $subcat->getImageUrl(); ?>

    <img src="<?php echo $this->helper('KetanresizeHelperImage')->imageResize($_imgUrl,'256','128','subtcat/'); ?>">

    <?php } ?>





    share|improve this answer













    Create resize image by passing image URL




    create Block file appcodeKetanResizeHelperImage.php



    pass url and dimension




    <?php
    /**
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */




    namespace KetanResizeHelper;

    use MagentoFrameworkFilesystem;
    use MagentoFrameworkUrl;
    use MagentoFrameworkAppHelperAbstractHelper;
    use MagentoFrameworkAppFilesystemDirectoryList;

    class Image extends MagentoFrameworkAppHelperAbstractHelper {
    protected $scopeConfig;
    protected $storeManager;
    protected $messageManager;
    protected $_response;
    protected $_resourceConfig;
    protected $_responseFactory;
    protected $_url;
    protected $_filesystem;
    protected $_directory;
    protected $_imageFactory;
    public function __construct(
    MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoFrameworkMessageManagerInterface $messageManager,
    MagentoFrameworkAppResponseInterface $response,
    MagentoFrameworkAppConfigStorageWriterInterface $resourceConfig,
    MagentoFrameworkAppResponseFactory $responseFactory,
    MagentoFrameworkUrlInterface $url,
    MagentoFrameworkFilesystem $filesystem,
    MagentoFrameworkImageAdapterFactory $imageFactory
    )
    {
    $this->scopeConfig = $scopeConfig;
    $this->_storeManager=$storeManager;
    $this->messageManager=$messageManager;
    $this->_response=$response;
    $this->_resourceConfig=$resourceConfig;
    $this->_responseFactory = $responseFactory;
    $this->_url = $url;
    $this->_filesystem = $filesystem;
    $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
    $this->_imageFactory = $imageFactory;
    }
    public function imageResize(
    $src,
    $width=35,
    $height=35,
    $dir='resize/'
    ){
    if (!@getimagesize($src)) {
    $src = $this->_storeManager->getStore()->getBaseUrl().'pub/media/catalog/product/placeholder/'.$this->scopeConfig->getValue('catalog/placeholder/small_image_placeholder',MagentoStoreModelScopeInterface::SCOPE_STORE);
    }
    $absPath = $src;
    $imageResized = $this->_filesystem
    ->getDirectoryRead(DirectoryList::MEDIA)
    ->getAbsolutePath($dir).
    $this->getNewDirectoryImage($src);
    $imageResize = $this->_imageFactory->create();
    $imageResize->open($absPath);
    $imageResize->backgroundColor([255, 255, 255]);
    $imageResize->constrainOnly(TRUE);
    $imageResize->keepTransparency(TRUE);
    $imageResize->keepFrame(true);
    $imageResize->keepAspectRatio(true);
    $imageResize->resize($width,$height);
    $dest = $imageResized ;
    $imageResize->save($dest);
    $resizedURL= $this->_storeManager->getStore()
    ->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).
    $dir.$this->getNewDirectoryImage($src);
    return $resizedURL;
    }
    public function getNewDirectoryImage($src){
    $segments = array_reverse(explode('/',$src));
    $first_dir = substr($segments[0],0,1);
    $second_dir = substr($segments[0],1,1);
    return 'cache/'.$first_dir.'/'.$second_dir.'/'.$segments[0];
    }
    }


    In your phtml file



    <?php foreach ($subcats as $subcat) { 
    $_imgUrl = $subcat->getImageUrl(); ?>

    <img src="<?php echo $this->helper('KetanresizeHelperImage')->imageResize($_imgUrl,'256','128','subtcat/'); ?>">

    <?php } ?>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 1 at 10:22









    Ketan BoradaKetan Borada

    406938




    406938













    • getting @Error filtering template: Warning: getimagesize(192.168.1.122/als2/pub/media/catalog/product/placeholder): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in D:xampphtdocsals2vendormagentoframeworkImageAdapterAbstractAdapter.php on line 304

      – bhakti thakkar
      Feb 1 at 10:57











    • upload place holder image in admin small_image_placeholder prntscr.com/mf6jlv f there is no image it will take from there

      – Ketan Borada
      Feb 1 at 11:02













    • I got resized img of placeholder but I want subcategory image

      – bhakti thakkar
      Feb 1 at 11:24











    • be sure are you passing category image url in $_imgUrl otherwise it will take place holder image

      – Ketan Borada
      Feb 1 at 11:26



















    • getting @Error filtering template: Warning: getimagesize(192.168.1.122/als2/pub/media/catalog/product/placeholder): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in D:xampphtdocsals2vendormagentoframeworkImageAdapterAbstractAdapter.php on line 304

      – bhakti thakkar
      Feb 1 at 10:57











    • upload place holder image in admin small_image_placeholder prntscr.com/mf6jlv f there is no image it will take from there

      – Ketan Borada
      Feb 1 at 11:02













    • I got resized img of placeholder but I want subcategory image

      – bhakti thakkar
      Feb 1 at 11:24











    • be sure are you passing category image url in $_imgUrl otherwise it will take place holder image

      – Ketan Borada
      Feb 1 at 11:26

















    getting @Error filtering template: Warning: getimagesize(192.168.1.122/als2/pub/media/catalog/product/placeholder): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in D:xampphtdocsals2vendormagentoframeworkImageAdapterAbstractAdapter.php on line 304

    – bhakti thakkar
    Feb 1 at 10:57





    getting @Error filtering template: Warning: getimagesize(192.168.1.122/als2/pub/media/catalog/product/placeholder): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in D:xampphtdocsals2vendormagentoframeworkImageAdapterAbstractAdapter.php on line 304

    – bhakti thakkar
    Feb 1 at 10:57













    upload place holder image in admin small_image_placeholder prntscr.com/mf6jlv f there is no image it will take from there

    – Ketan Borada
    Feb 1 at 11:02







    upload place holder image in admin small_image_placeholder prntscr.com/mf6jlv f there is no image it will take from there

    – Ketan Borada
    Feb 1 at 11:02















    I got resized img of placeholder but I want subcategory image

    – bhakti thakkar
    Feb 1 at 11:24





    I got resized img of placeholder but I want subcategory image

    – bhakti thakkar
    Feb 1 at 11:24













    be sure are you passing category image url in $_imgUrl otherwise it will take place holder image

    – Ketan Borada
    Feb 1 at 11:26





    be sure are you passing category image url in $_imgUrl otherwise it will take place holder image

    – Ketan Borada
    Feb 1 at 11:26













    2














    Please try this code for resized any images.



    Using $objectManager :



    $objectManager = MagentoFrameworkAppObjectManager::getInstance();

    $directory = $objectManager->get('MagentoFrameworkFilesystemDirectoryList');
    $storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
    $fileSystem = $objectManager->get('MagentoFrameworkFilesystem');

    $absolutePath = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$subcat->getUrl();
    $imageResized = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$subcat->getUrl();

    //create image factory...
    $imageResize = $objectManager->create('MagentoFrameworkImageAdapterFactory');
    $imageResize->open($absolutePath);
    $imageResize->constrainOnly(TRUE);
    $imageResize->keepTransparency(TRUE);
    $imageResize->keepFrame(FALSE);
    $imageResize->keepAspectRatio(TRUE);
    $imageResize->resize($width,$height);
    //destination folder
    $destination = $imageResized;
    //save image
    $imageResize->save($destination);

    $resizedURL = $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

    echo $resizedURL;


    Using Helper :



    <?php
    namespace VendorModuleHelper;

    use MagentoStoreModelStoreManagerInterface;

    class Imageresize extends MagentoFrameworkAppHelperAbstractHelper
    {
    protected $_filesystem ;
    protected $_imageFactory;
    protected $_storeManager;
    protected $_objectManager;

    public function __construct(
    MagentoFrameworkFilesystem $filesystem,
    MagentoFrameworkObjectManagerInterface $objectManager,
    StoreManagerInterface $storeManager,
    MagentoFrameworkImageAdapterFactory $imageFactory
    ) {
    $this->_filesystem = $filesystem;
    $this->_imageFactory = $imageFactory;
    $this->_objectManager = $objectManager;
    $this->_storeManager = $storeManager;
    }

    // pass imagename, width and height
    public function resize($image, $width = null, $height = null)
    {
    $absolutePath = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$image;

    $imageResized = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image;
    //create image factory...
    $imageResize = $this->_imageFactory->create();
    $imageResize->open($absolutePath);
    $imageResize->constrainOnly(TRUE);
    $imageResize->keepTransparency(TRUE);
    $imageResize->keepFrame(FALSE);
    $imageResize->keepAspectRatio(TRUE);
    $imageResize->resize($width,$height);
    //destination folder
    $destination = $imageResized;
    //save image
    $imageResize->save($destination);

    $resizedURL = $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

    return $resizedURL;
    }
    }
    ?>


    And then add following code in your .phtml



    <?php $imgResized = $this->helper('VendorModuleHelperImageresize')->resize($subcaturl,277,180); ?>
    <a href="<?php echo $br['link']; ?>"><img src="<?php echo $imgResized; ?>" /></a>


    Please check and let me know if any issue.






    share|improve this answer






























      2














      Please try this code for resized any images.



      Using $objectManager :



      $objectManager = MagentoFrameworkAppObjectManager::getInstance();

      $directory = $objectManager->get('MagentoFrameworkFilesystemDirectoryList');
      $storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
      $fileSystem = $objectManager->get('MagentoFrameworkFilesystem');

      $absolutePath = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$subcat->getUrl();
      $imageResized = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$subcat->getUrl();

      //create image factory...
      $imageResize = $objectManager->create('MagentoFrameworkImageAdapterFactory');
      $imageResize->open($absolutePath);
      $imageResize->constrainOnly(TRUE);
      $imageResize->keepTransparency(TRUE);
      $imageResize->keepFrame(FALSE);
      $imageResize->keepAspectRatio(TRUE);
      $imageResize->resize($width,$height);
      //destination folder
      $destination = $imageResized;
      //save image
      $imageResize->save($destination);

      $resizedURL = $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

      echo $resizedURL;


      Using Helper :



      <?php
      namespace VendorModuleHelper;

      use MagentoStoreModelStoreManagerInterface;

      class Imageresize extends MagentoFrameworkAppHelperAbstractHelper
      {
      protected $_filesystem ;
      protected $_imageFactory;
      protected $_storeManager;
      protected $_objectManager;

      public function __construct(
      MagentoFrameworkFilesystem $filesystem,
      MagentoFrameworkObjectManagerInterface $objectManager,
      StoreManagerInterface $storeManager,
      MagentoFrameworkImageAdapterFactory $imageFactory
      ) {
      $this->_filesystem = $filesystem;
      $this->_imageFactory = $imageFactory;
      $this->_objectManager = $objectManager;
      $this->_storeManager = $storeManager;
      }

      // pass imagename, width and height
      public function resize($image, $width = null, $height = null)
      {
      $absolutePath = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$image;

      $imageResized = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image;
      //create image factory...
      $imageResize = $this->_imageFactory->create();
      $imageResize->open($absolutePath);
      $imageResize->constrainOnly(TRUE);
      $imageResize->keepTransparency(TRUE);
      $imageResize->keepFrame(FALSE);
      $imageResize->keepAspectRatio(TRUE);
      $imageResize->resize($width,$height);
      //destination folder
      $destination = $imageResized;
      //save image
      $imageResize->save($destination);

      $resizedURL = $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

      return $resizedURL;
      }
      }
      ?>


      And then add following code in your .phtml



      <?php $imgResized = $this->helper('VendorModuleHelperImageresize')->resize($subcaturl,277,180); ?>
      <a href="<?php echo $br['link']; ?>"><img src="<?php echo $imgResized; ?>" /></a>


      Please check and let me know if any issue.






      share|improve this answer




























        2












        2








        2







        Please try this code for resized any images.



        Using $objectManager :



        $objectManager = MagentoFrameworkAppObjectManager::getInstance();

        $directory = $objectManager->get('MagentoFrameworkFilesystemDirectoryList');
        $storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
        $fileSystem = $objectManager->get('MagentoFrameworkFilesystem');

        $absolutePath = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$subcat->getUrl();
        $imageResized = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$subcat->getUrl();

        //create image factory...
        $imageResize = $objectManager->create('MagentoFrameworkImageAdapterFactory');
        $imageResize->open($absolutePath);
        $imageResize->constrainOnly(TRUE);
        $imageResize->keepTransparency(TRUE);
        $imageResize->keepFrame(FALSE);
        $imageResize->keepAspectRatio(TRUE);
        $imageResize->resize($width,$height);
        //destination folder
        $destination = $imageResized;
        //save image
        $imageResize->save($destination);

        $resizedURL = $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

        echo $resizedURL;


        Using Helper :



        <?php
        namespace VendorModuleHelper;

        use MagentoStoreModelStoreManagerInterface;

        class Imageresize extends MagentoFrameworkAppHelperAbstractHelper
        {
        protected $_filesystem ;
        protected $_imageFactory;
        protected $_storeManager;
        protected $_objectManager;

        public function __construct(
        MagentoFrameworkFilesystem $filesystem,
        MagentoFrameworkObjectManagerInterface $objectManager,
        StoreManagerInterface $storeManager,
        MagentoFrameworkImageAdapterFactory $imageFactory
        ) {
        $this->_filesystem = $filesystem;
        $this->_imageFactory = $imageFactory;
        $this->_objectManager = $objectManager;
        $this->_storeManager = $storeManager;
        }

        // pass imagename, width and height
        public function resize($image, $width = null, $height = null)
        {
        $absolutePath = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$image;

        $imageResized = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image;
        //create image factory...
        $imageResize = $this->_imageFactory->create();
        $imageResize->open($absolutePath);
        $imageResize->constrainOnly(TRUE);
        $imageResize->keepTransparency(TRUE);
        $imageResize->keepFrame(FALSE);
        $imageResize->keepAspectRatio(TRUE);
        $imageResize->resize($width,$height);
        //destination folder
        $destination = $imageResized;
        //save image
        $imageResize->save($destination);

        $resizedURL = $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

        return $resizedURL;
        }
        }
        ?>


        And then add following code in your .phtml



        <?php $imgResized = $this->helper('VendorModuleHelperImageresize')->resize($subcaturl,277,180); ?>
        <a href="<?php echo $br['link']; ?>"><img src="<?php echo $imgResized; ?>" /></a>


        Please check and let me know if any issue.






        share|improve this answer















        Please try this code for resized any images.



        Using $objectManager :



        $objectManager = MagentoFrameworkAppObjectManager::getInstance();

        $directory = $objectManager->get('MagentoFrameworkFilesystemDirectoryList');
        $storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
        $fileSystem = $objectManager->get('MagentoFrameworkFilesystem');

        $absolutePath = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$subcat->getUrl();
        $imageResized = $objectManager->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$subcat->getUrl();

        //create image factory...
        $imageResize = $objectManager->create('MagentoFrameworkImageAdapterFactory');
        $imageResize->open($absolutePath);
        $imageResize->constrainOnly(TRUE);
        $imageResize->keepTransparency(TRUE);
        $imageResize->keepFrame(FALSE);
        $imageResize->keepAspectRatio(TRUE);
        $imageResize->resize($width,$height);
        //destination folder
        $destination = $imageResized;
        //save image
        $imageResize->save($destination);

        $resizedURL = $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

        echo $resizedURL;


        Using Helper :



        <?php
        namespace VendorModuleHelper;

        use MagentoStoreModelStoreManagerInterface;

        class Imageresize extends MagentoFrameworkAppHelperAbstractHelper
        {
        protected $_filesystem ;
        protected $_imageFactory;
        protected $_storeManager;
        protected $_objectManager;

        public function __construct(
        MagentoFrameworkFilesystem $filesystem,
        MagentoFrameworkObjectManagerInterface $objectManager,
        StoreManagerInterface $storeManager,
        MagentoFrameworkImageAdapterFactory $imageFactory
        ) {
        $this->_filesystem = $filesystem;
        $this->_imageFactory = $imageFactory;
        $this->_objectManager = $objectManager;
        $this->_storeManager = $storeManager;
        }

        // pass imagename, width and height
        public function resize($image, $width = null, $height = null)
        {
        $absolutePath = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::ROOT)->getAbsolutePath('pub/media/').$image;

        $imageResized = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA)->getAbsolutePath('resized/'.$width.'/').$image;
        //create image factory...
        $imageResize = $this->_imageFactory->create();
        $imageResize->open($absolutePath);
        $imageResize->constrainOnly(TRUE);
        $imageResize->keepTransparency(TRUE);
        $imageResize->keepFrame(FALSE);
        $imageResize->keepAspectRatio(TRUE);
        $imageResize->resize($width,$height);
        //destination folder
        $destination = $imageResized;
        //save image
        $imageResize->save($destination);

        $resizedURL = $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA).'resized/'.$width.'/'.$image;

        return $resizedURL;
        }
        }
        ?>


        And then add following code in your .phtml



        <?php $imgResized = $this->helper('VendorModuleHelperImageresize')->resize($subcaturl,277,180); ?>
        <a href="<?php echo $br['link']; ?>"><img src="<?php echo $imgResized; ?>" /></a>


        Please check and let me know if any issue.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 1 at 12:41

























        answered Feb 1 at 10:33









        Aasim GoriyaAasim Goriya

        4,0131938




        4,0131938






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f260123%2fhow-to-resize-image-in-magento-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]