diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery/Upload.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery/Upload.php index d43b313c43b3e..3b736b76c641f 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery/Upload.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery/Upload.php @@ -9,6 +9,7 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; +use Magento\Backend\Model\Image\UploadResizeConfigInterface; /** * Class Upload @@ -27,6 +28,16 @@ class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterf */ protected $resultRawFactory; + /** + * @var \Magento\MediaStorage\Model\File\UploaderFactory + */ + private $uploaderFactory; + + /** + * @var \Magento\Framework\Filesystem\Directory\WriteInterface + */ + protected $mediaDirectory; + /** * @var array */ @@ -52,28 +63,39 @@ class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterf */ private $productMediaConfig; + /** + * @var \Magento\Backend\Model\Image\UploadResizeConfigInterface + */ + private $imageUploadConfig; + /** * @param \Magento\Backend\App\Action\Context $context * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory - * @param \Magento\Framework\Image\AdapterFactory $adapterFactory + * @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory * @param \Magento\Framework\Filesystem $filesystem + * @param \Magento\Framework\Image\AdapterFactory $adapterFactory * @param \Magento\Catalog\Model\Product\Media\Config $productMediaConfig + * @param UploadResizeConfigInterface $imageUploadConfig */ public function __construct( \Magento\Backend\App\Action\Context $context, \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, + \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, + \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Image\AdapterFactory $adapterFactory = null, - \Magento\Framework\Filesystem $filesystem = null, - \Magento\Catalog\Model\Product\Media\Config $productMediaConfig = null + \Magento\Catalog\Model\Product\Media\Config $productMediaConfig = null, + UploadResizeConfigInterface $imageUploadConfig = null ) { parent::__construct($context); $this->resultRawFactory = $resultRawFactory; + $this->uploaderFactory = $uploaderFactory; + $this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA); $this->adapterFactory = $adapterFactory ?: ObjectManager::getInstance() ->get(\Magento\Framework\Image\AdapterFactory::class); - $this->filesystem = $filesystem ?: ObjectManager::getInstance() - ->get(\Magento\Framework\Filesystem::class); $this->productMediaConfig = $productMediaConfig ?: ObjectManager::getInstance() ->get(\Magento\Catalog\Model\Product\Media\Config::class); + $this->imageUploadConfig = $imageUploadConfig ?: ObjectManager::getInstance() + ->get(UploadResizeConfigInterface::class); } /** @@ -84,20 +106,16 @@ public function __construct( public function execute() { try { - $uploader = $this->_objectManager->create( - \Magento\MediaStorage\Model\File\Uploader::class, - ['fileId' => 'image'] - ); + $uploader = $this->uploaderFactory->create(['fileId' => 'image']); $uploader->setAllowedExtensions($this->getAllowedExtensions()); $imageAdapter = $this->adapterFactory->create(); $uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile'); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); - $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); $result = $uploader->save( - $mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath()) + $this->mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath()) ); - + $this->resizeImage($imageAdapter, $result['path'], $result['file']); $this->_eventManager->dispatch( 'catalog_product_gallery_upload_image_after', ['result' => $result, 'action' => $this] @@ -128,4 +146,32 @@ private function getAllowedExtensions() { return array_keys($this->allowedMimeTypes); } + + /** + * Resize the image + * + * @param \Magento\Framework\Image\AdapterFactory $imageAdapter + * @param string $path + * @param string $file + * @return bool + */ + private function resizeImage($imageAdapter, $path, $file) + { + try { + # Get width and height of the uploaded image + list($imageWidth, $imageHeight) = getimagesize($path . $file); + if ($this->imageUploadConfig->isResizeEnabled() + && $imageHeight > $this->imageUploadConfig->getMaxHeight() + || $imageWidth > $this->imageUploadConfig->getMaxWidth() + ) { + $imageAdapter->open($path . $file); + $imageAdapter->keepAspectRatio(true); + $imageAdapter->resize($this->imageUploadConfig->getMaxWidth(), $this->imageUploadConfig->getMaxHeight()); + $imageAdapter->save(); + } + return true; + } catch (\Exception $e) { + return false; + } + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Gallery/UploadTest.php b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Gallery/UploadTest.php new file mode 100644 index 0000000000000..66145cd17a65f --- /dev/null +++ b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Gallery/UploadTest.php @@ -0,0 +1,113 @@ +resultRawFactory = $this->createMock( + \Magento\Framework\Controller\Result\RawFactory::class + ); + $this->uploaderFactory = $this->createMock( + \Magento\MediaStorage\Model\File\UploaderFactory::class + ); + $filesystem = $this->createMock( + \Magento\Framework\Filesystem::class + ); + $this->mediaDirectory = $this->createMock( + \Magento\Framework\Filesystem\Directory\WriteInterface::class + ); + $filesystem->expects($this->any())->method('getDirectoryWrite')->willReturn( + $this->mediaDirectory + ); + $this->adapterFactory = $this->createMock( + \Magento\Framework\Image\AdapterFactory::class + ); + $this->productMediaConfig = $this->createMock( + \Magento\Catalog\Model\Product\Media\Config::class + ); + $this->imageUploadConfig = $this->createMock( + \Magento\Backend\Model\Image\UploadResizeConfigInterface::class + ); + $this->baseTmpPath = 'base/tmp/'; + $this->basePath = 'base/real/'; + $this->url = 'http://local.magento2.com/media/tmp/catalog/product/f/i/file.png'; + $this->allowedExtensions = [ + 'jpg' => 'image/jpg', + 'jpeg' => 'image/jpeg', + 'gif' => 'image/png', + 'png' => 'image/gif' + ]; + $this->result= [ + 'file' => 'file.png.tmp', + 'url' => $this->url + ]; + $this->fileId = 'file.png'; + $this->upload = $objectManager->getObject( + \Magento\Catalog\Controller\Adminhtml\Product\Gallery\Upload::class, + [ + 'resultRawFactory' => $this->resultRawFactory, + 'uploaderFactory' => $this->uploaderFactory, + 'mediaDirectory' => $this->mediaDirectory, + 'adapterFactory' => $this->adapterFactory, + 'productMediaConfig' => $this->productMediaConfig, + 'imageUploadConfig' => $this->imageUploadConfig + ] + ); + } + + public function testExecute() + { + $uploader = $this->createMock(\Magento\MediaStorage\Model\File\Uploader::class); + $this->uploaderFactory->expects($this->once())->method('create')->willReturn($uploader); + $uploader->expects($this->once())->method('setAllowedExtensions')->with(array_keys($this->allowedExtensions)); + $uploader->expects($this->once())->method('setAllowRenameFiles')->with(true); + $this->productMediaConfig->expects($this->once())->method('getBaseTmpMediaPath')->willReturn($this->baseTmpPath); + $this->mediaDirectory->expects($this->once())->method('getAbsolutePath')->with($this->baseTmpPath) + ->willReturn($this->basePath); + $uploader->expects($this->once())->method('save')->with($this->basePath) + ->willReturn(['tmp_name' => $this->baseTmpPath, 'file' => $this->fileId, 'path' => $this->basePath]); + $this->productMediaConfig->expects($this->once())->method('getTmpMediaUrl')->willReturn($this->url); + $response = $this->createMock(\Magento\Framework\Controller\Result\Raw::class); + $this->resultRawFactory->expects($this->once())->method('create')->willReturn($response); + $response->expects($this->once())->method('setHeader')->with('Content-type', 'text/plain'); + $response->expects($this->once())->method('setContents')->with(json_encode($this->result)); + $this->upload->execute(); + } +}