Example #1
0
 /**
  * Image upload action in iframe
  *
  * @return string
  */
 public function execute()
 {
     try {
         $uploader = $this->uploaderFactory->create(['fileId' => 'datafile']);
         $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
         /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
         $imageAdapter = $this->adapterFactory->create();
         $uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
         /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
         $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
         $config = $this->config;
         $result = $uploader->save($mediaDirectory->getAbsolutePath($config->getBaseTmpMediaPath()));
         $this->_eventManager->dispatch('swatch_gallery_upload_image_after', ['result' => $result, 'action' => $this]);
         unset($result['tmp_name']);
         unset($result['path']);
         $result['url'] = $this->config->getTmpMediaUrl($result['file']);
         $result['file'] = $result['file'] . '.tmp';
         $newFile = $this->swatchHelper->moveImageFromTmp($result['file']);
         $this->swatchHelper->generateSwatchVariations($newFile);
         $fileData = ['swatch_path' => $this->swatchHelper->getSwatchMediaUrl(), 'file_path' => $newFile];
         $this->getResponse()->setBody(json_encode($fileData));
     } catch (\Exception $e) {
         $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
         $this->getResponse()->setBody(json_encode($result));
     }
 }
Example #2
0
 /**
  * Checks if chosen image adapter available
  *
  * @throws \Magento\Framework\Model\Exception If some of adapter dependencies was not loaded
  * @return \Magento\Backend\Model\Config\Backend\File
  */
 protected function _beforeSave()
 {
     try {
         $this->_imageFactory->create($this->getValue());
     } catch (\Exception $e) {
         $message = __('The specified image adapter cannot be used because of: ' . $e->getMessage());
         throw new \Magento\Framework\Model\Exception($message);
     }
     return $this;
 }
Example #3
0
 /**
  * Initiate uploader defoult settings
  *
  * @return void
  */
 public function init()
 {
     $this->setAllowRenameFiles(true);
     $this->setAllowCreateFolders(true);
     $this->setFilesDispersion(true);
     $this->setAllowedExtensions(array_keys($this->_allowedMimeTypes));
     $imageAdapter = $this->_imageFactory->create();
     $this->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
     $this->_uploadType = self::SINGLE_STYLE;
 }
Example #4
0
 /**
  * Checks if chosen image adapter available
  *
  * @throws \Magento\Framework\Exception\LocalizedException If some of adapter dependencies was not loaded
  * @return \Magento\Config\Model\Config\Backend\File
  */
 public function beforeSave()
 {
     try {
         $this->_imageFactory->create($this->getValue());
     } catch (\Exception $e) {
         $message = __('The specified image adapter cannot be used because of: %1', $e->getMessage());
         throw new \Magento\Framework\Exception\LocalizedException($message);
     }
     return $this;
 }
 /**
  * @param \Magento\Backend\App\Action\Context $context
  * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
  * @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
  * @param \Magento\Framework\Filesystem $fileSystem
  * @param \Magento\Framework\Image\AdapterFactory $imageAdapterFactory
  * @param \Magento\Framework\HTTP\Adapter\Curl $curl
  * @param \Magento\MediaStorage\Model\ResourceModel\File\Storage\File $fileUtility
  */
 public function __construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, \Magento\Catalog\Model\Product\Media\Config $mediaConfig, \Magento\Framework\Filesystem $fileSystem, \Magento\Framework\Image\AdapterFactory $imageAdapterFactory, \Magento\Framework\HTTP\Adapter\Curl $curl, \Magento\MediaStorage\Model\ResourceModel\File\Storage\File $fileUtility)
 {
     parent::__construct($context);
     $this->resultRawFactory = $resultRawFactory;
     $this->mediaConfig = $mediaConfig;
     $this->fileSystem = $fileSystem;
     $this->imageAdapter = $imageAdapterFactory->create();
     $this->curl = $curl;
     $this->fileUtility = $fileUtility;
 }
Example #6
0
 /**
  * @dataProvider dataForExecute
  */
 public function testExecute($fileResult, $expectedResult)
 {
     $this->uploaderFactoryMock->expects($this->once())->method('create')->willReturn($this->uploaderMock);
     $this->adapterFactoryMock->expects($this->once())->method('create')->willReturn($this->adapterMock);
     $this->filesystemMock->expects($this->once())->method('getDirectoryRead')->with('media')->willReturn($this->mediaDirectoryMock);
     $this->uploaderMock->expects($this->once())->method('save')->willReturn($fileResult);
     $this->configMock->expects($this->once())->method('getTmpMediaUrl')->with($fileResult['file'])->willReturn('http://domain.com/tpm_dir/m/a/magento.png');
     $this->swatchHelperMock->expects($this->once())->method('moveImageFromTmp')->with('/m/a/magento.png.tmp')->willReturn('/m/a/magento.png');
     $this->swatchHelperMock->expects($this->once())->method('generateSwatchVariations');
     $this->swatchHelperMock->expects($this->once())->method('getSwatchMediaUrl')->willReturn('http://domain.com/media/path/');
     $this->responseMock->expects($this->once())->method('setBody')->willReturn(json_encode($expectedResult));
     $this->controller->execute();
 }
Example #7
0
 /**
  * Create thumbnail for image and save it to thumbnails directory
  *
  * @param string $source Image path to be resized
  * @param bool $keepRation Keep aspect ratio or not
  * @return bool|string Resized filepath or false if errors were occurred
  */
 public function resizeFile($source, $keepRation = true)
 {
     $realPath = $this->_directory->getRelativePath($source);
     if (!$this->_directory->isFile($realPath) || !$this->_directory->isExist($realPath)) {
         return false;
     }
     $targetDir = $this->getThumbsPath($source);
     $pathTargetDir = $this->_directory->getRelativePath($targetDir);
     if (!$this->_directory->isExist($pathTargetDir)) {
         $this->_directory->create($pathTargetDir);
     }
     if (!$this->_directory->isExist($pathTargetDir)) {
         return false;
     }
     $image = $this->_imageFactory->create();
     $image->open($source);
     $image->keepAspectRatio($keepRation);
     $image->resize($this->_resizeParameters['width'], $this->_resizeParameters['height']);
     $dest = $targetDir . '/' . pathinfo($source, PATHINFO_BASENAME);
     $image->save($dest);
     if ($this->_directory->isFile($this->_directory->getRelativePath($dest))) {
         return $dest;
     }
     return false;
 }
 /**
  * Set up
  */
 public function setUp()
 {
     $this->contextMock = $this->getMock('\\Magento\\Backend\\App\\Action\\Context', [], [], '', false);
     $this->rawFactoryMock = $this->getMock('\\Magento\\Framework\\Controller\\Result\\RawFactory', ['create'], [], '', false);
     $response = $this->getMock('\\Magento\\Framework\\Controller\\Result\\Raw', [], [], '', false);
     $this->rawFactoryMock->expects($this->once())->method('create')->willReturn($response);
     $this->configMock = $this->getMock('\\Magento\\Catalog\\Model\\Product\\Media\\Config', [], [], '', false);
     $this->filesystemMock = $this->getMock('\\Magento\\Framework\\Filesystem', [], [], '', false);
     $this->adapterMock = $this->getMock('\\Magento\\Framework\\Image', [], [], '', false);
     $this->adapterFactoryMock = $this->getMock('\\Magento\\Framework\\Image\\AdapterFactory', ['create'], [], '', false);
     $this->abstractAdapter = $this->getMock('\\Magento\\Framework\\Image\\Adapter\\AbstractAdapter', [], [], '', false);
     $this->adapterFactoryMock->expects($this->once())->method('create')->willReturn($this->abstractAdapter);
     $this->curlMock = $this->getMock('\\Magento\\Framework\\HTTP\\Adapter\\Curl', [], [], '', false);
     $this->storageFileMock = $this->getMock('\\Magento\\MediaStorage\\Model\\ResourceModel\\File\\Storage\\File', [], [], '', false);
     $this->request = $this->getMock('\\Magento\\Framework\\App\\RequestInterface');
     $this->contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
     $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
     $this->image = $objectManager->getObject('\\Magento\\ProductVideo\\Controller\\Adminhtml\\Product\\Gallery\\RetrieveImage', ['context' => $this->contextMock, 'resultRawFactory' => $this->rawFactoryMock, 'mediaConfig' => $this->configMock, 'fileSystem' => $this->filesystemMock, 'imageAdapterFactory' => $this->adapterFactoryMock, 'curl' => $this->curlMock, 'fileUtility' => $this->storageFileMock]);
 }
Example #9
0
 protected function prepareExecuteTest()
 {
     $directiveParam = 'e3ttZWRpYSB1cmw9Ind5c2l3eWcvYnVubnkuanBnIn19';
     $directive = '{{media url="wysiwyg/image.jpg"}}';
     $this->requestMock->expects($this->once())->method('getParam')->with('___directive')->willReturn($directiveParam);
     $this->urlDecoderMock->expects($this->once())->method('decode')->with($directiveParam)->willReturn($directive);
     $this->objectManagerMock->expects($this->once())->method('create')->with('Magento\\Cms\\Model\\Template\\Filter')->willReturn($this->templateFilterMock);
     $this->templateFilterMock->expects($this->once())->method('filter')->with($directive)->willReturn(self::IMAGE_PATH);
     $this->objectManagerMock->expects($this->any())->method('get')->willReturnMap([['Magento\\Framework\\Image\\AdapterFactory', $this->imageAdapterFactoryMock], ['Magento\\Cms\\Model\\Wysiwyg\\Config', $this->wysiwygConfigMock], ['Psr\\Log\\LoggerInterface', $this->loggerMock]]);
     $this->imageAdapterFactoryMock->expects($this->once())->method('create')->willReturn($this->imageAdapterMock);
 }
 /**
  * cover \Magento\Theme\Model\Wysiwyg\Storage::_createThumbnail
  * cover \Magento\Theme\Model\Wysiwyg\Storage::uploadFile
  */
 public function testUploadFile()
 {
     $uploader = $this->_prepareUploader();
     $uploader->expects($this->once())->method('save')->will($this->returnValue(['not_empty']));
     $this->_helperStorage->expects($this->once())->method('getStorageType')->will($this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE));
     /** Prepare filesystem */
     $this->directoryWrite->expects($this->any())->method('isFile')->will($this->returnValue(true));
     $this->directoryWrite->expects($this->once())->method('isReadable')->will($this->returnValue(true));
     /** Prepare image */
     $image = $this->getMock('Magento\\Framework\\Image\\Adapter\\Gd2', [], [], '', false);
     $image->expects($this->once())->method('open')->will($this->returnValue(true));
     $image->expects($this->once())->method('keepAspectRatio')->will($this->returnValue(true));
     $image->expects($this->once())->method('resize')->will($this->returnValue(true));
     $image->expects($this->once())->method('save')->will($this->returnValue(true));
     $this->_imageFactory->expects($this->at(0))->method('create')->will($this->returnValue($image));
     /** Prepare session */
     $session = $this->getMock('Magento\\Backend\\Model\\Session', [], [], '', false);
     $this->_helperStorage->expects($this->any())->method('getSession')->will($this->returnValue($session));
     $expectedResult = ['not_empty', 'cookie' => ['name' => null, 'value' => null, 'lifetime' => null, 'path' => null, 'domain' => null]];
     $this->assertEquals($expectedResult, $this->_storageModel->uploadFile($this->_storageRoot));
 }
Example #11
0
 /**
  * Create thumbnail for image and save it to thumbnails directory
  *
  * @param string $source
  * @return bool|string Resized filepath or false if errors were occurred
  */
 public function _createThumbnail($source)
 {
     if (self::TYPE_IMAGE != $this->_helper->getStorageType() || !$this->mediaWriteDirectory->isFile($source) || !$this->mediaWriteDirectory->isReadable($source)) {
         return false;
     }
     $thumbnailDir = $this->_helper->getThumbnailDirectory($source);
     $thumbnailPath = $thumbnailDir . '/' . pathinfo($source, PATHINFO_BASENAME);
     try {
         $this->mediaWriteDirectory->isExist($thumbnailDir);
         $image = $this->_imageFactory->create();
         $image->open($this->mediaWriteDirectory->getAbsolutePath($source));
         $image->keepAspectRatio(true);
         $image->resize(self::THUMBNAIL_WIDTH, self::THUMBNAIL_HEIGHT);
         $image->save($this->mediaWriteDirectory->getAbsolutePath($thumbnailPath));
     } catch (\Magento\Framework\Exception\FileSystemException $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
         return false;
     }
     if ($this->mediaWriteDirectory->isFile($thumbnailPath)) {
         return $thumbnailPath;
     }
     return false;
 }
 /**
  * @covers \Magento\Framework\Image\AdapterFactory::create
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage stdClass is not instance of \Magento\Framework\Image\Adapter\AdapterInterface
  */
 public function testWrongInstance()
 {
     $alias = 'wrongInstance';
     $class = 'stdClass';
     $objectManagerMock = $this->getMock('Magento\\Framework\\ObjectManager\\ObjectManager', ['create'], [], '', false);
     $imageAdapterMock = $this->getMock($class, ['checkDependencies']);
     $objectManagerMock->expects($this->once())->method('create')->with($class)->will($this->returnValue($imageAdapterMock));
     $adapterFactory = new AdapterFactory($objectManagerMock, $this->configMock);
     $adapterFactory->create($alias);
 }
Example #13
0
 /**
  * Create instance of \Magento\Framework\Image
  *
  * @param string|null $fileName
  * @param string|null $adapterName
  * @return \Magento\Framework\Image
  */
 public function create($fileName = null, $adapterName = null)
 {
     $adapter = $this->adapterFactory->create($adapterName);
     return $this->objectManager->create('Magento\\Framework\\Image', array('adapter' => $adapter, 'fileName' => $fileName));
 }