#StackBounty: #magento2 #image-upload #image-resize magento 2 : resize image before upload on destination path

Bounty: 100

I want to upload images for custom module, so before upload image on media folder I want that resize image.
How can I set resize image before upload on media folder ?

Here is my code:

public function __construct(
        MagentoFrameworkAppActionContext $context,
        MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        PsrLogLoggerInterface $loggerInterface,
        MagentoStoreModelStoreManagerInterface $storeManager,
        Filesystem $filesystem,
        FileFactory $fileFactory,
        MagentoMediaStorageModelFileUploaderFactory $uploaderFactory,
        MagentoFrameworkImageAdapterFactory $adapterFactory,
        MagentoFrameworkImageAdapterFactory $imageFactory,

       
        array $data = []
         
        )
    {
        $this->_inlineTranslation = $inlineTranslation;
        $this->_scopeConfig = $scopeConfig;
        $this->_logLoggerInterface = $loggerInterface;
        $this->messageManager = $context->getMessageManager();
        $this->_storeManager=$storeManager;
        $this->filesystem = $filesystem;
        $this->mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
        $this->fileFactory = $fileFactory;
        $this->uploaderFactory = $uploaderFactory;
        $this->adapterFactory = $adapterFactory;
       $this->imageFactory = $imageFactory;
        
        parent::__construct($context);
         
         
    }

    
    
    public function execute()
    {   
         try {$files = $this->getRequest()->getFiles('image');
    //echo "<pre>";print_r($files);die();
    
            foreach($files as $key => $value){
            $imageAdapter = $this->adapterFactory->create();
            $imageAdapter->open($value["tmp_name"]);
            $imageAdapter->constrainOnly(TRUE);        
            $imageAdapter->keepTransparency(TRUE);        
            $imageAdapter->keepFrame(FALSE);        
            $imageAdapter->keepAspectRatio(TRUE);        
            $imageAdapter->resize(100,100); 
             $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
            $imagepathfolder= 'test/';
            $destinationPath = $mediaDirectory->getAbsolutePath($imagepathfolder);
            $imageAdapter->save($destinationPath);
            
}echo "<pre>";print_r($result);die();
        } catch (Exception $e) {
        echo $e->getMessage();die();
        }
            
        
        /* $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        $resultJson->setData($printPath);
        return $resultJson; */
        }


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.