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(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Psr\Log\LoggerInterface $loggerInterface,
\Magento\Store\Model\StoreManagerInterface $storeManager,
Filesystem $filesystem,
FileFactory $fileFactory,
\Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
\Magento\Framework\Image\AdapterFactory $adapterFactory,
\Magento\Framework\Image\AdapterFactory $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(\Magento\Framework\App\Filesystem\DirectoryList::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; */
}
I am getting error when I run above my code Warning: imagejpeg(C:/wamp64/www/magento234/pub/media/test): failed to open stream: Permission denied in C:\wamp64\www\magento234\vendor\magento\framework\Image\Adapter\Gd2.php on line 208
What I want:
I have created a custom module and I have set one custom form with some fields. In this form I have set file input type in the form and other input fields, so when customer filled up form with image by file input and click on submit button ,it called JavaScript method and trigger ajax and call controller method where I put my above code.When I receive form elements' values as a post method in controller that time I want that image as resize image before upload process of that image.
Note
I DO NOT WANT TO UPLOAD THAT IMAGE IN ORIGINAL SIZE AND RESIZE THAT IMAGE AND UPLOAD ON OTHER PATH. I WANT THAT IMAGE FIRST RESIZE THEN IT SHOULD UPLOAD ON SERVER
C:/wamp64/www/magento234/pub/media/test
it looks like it's just a permissions issue.