/** * 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)); } }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * @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; }
/** * 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); }
/** * 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)); }