mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); $this->rootDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); $this->imageFactory = $imageFactory; $this->uploader = $uploader; $this->themeImagePath = $themeImagePath; $this->logger = $logger; $this->imageParams = $imageParams; $this->theme = $theme; } // @codingStandardsIgnoreEnd /** * Create preview image * * @param string $imagePath * @return $this */ public function createPreviewImage($imagePath) { list($imageWidth, $imageHeight) = $this->imageParams; $image = $this->imageFactory->create($imagePath); $image->keepTransparency(true); $image->constrainOnly(true); $image->keepFrame(true); $image->keepAspectRatio(true); $image->backgroundColor([255, 255, 255]); $image->resize($imageWidth, $imageHeight); $imageName = uniqid('preview_image_') . image_type_to_extension($image->getImageType()); $image->save($this->themeImagePath->getImagePreviewDirectory(), $imageName); $this->theme->setPreviewImage($imageName); return $this; } /** * Create preview image duplicate * * @param ThemeInterface $theme * @return bool */ public function createPreviewImageCopy(ThemeInterface $theme) { $previewDir = $this->themeImagePath->getImagePreviewDirectory(); $sourcePath = $theme->getThemeImage()->getPreviewImagePath(); $sourceRelativePath = $this->rootDirectory->getRelativePath($sourcePath); if (!$theme->getPreviewImage() && !$this->mediaDirectory->isExist($sourceRelativePath)) { return false; } $isCopied = false; try { $destinationFileName = \Magento\Framework\File\Uploader::getNewFileName($sourcePath); $targetRelativePath = $this->mediaDirectory->getRelativePath($previewDir . '/' . $destinationFileName); $isCopied = $this->rootDirectory->copyFile($sourceRelativePath, $targetRelativePath, $this->mediaDirectory); $this->theme->setPreviewImage($destinationFileName); } catch (\Magento\Framework\Exception\FileSystemException $e) { $this->theme->setPreviewImage(null); $this->logger->critical($e); } return $isCopied; } /** * Delete preview image * * @return bool */ public function removePreviewImage() { $previewImage = $this->theme->getPreviewImage(); $this->theme->setPreviewImage(null); if ($previewImage) { $previewImagePath = $this->themeImagePath->getImagePreviewDirectory() . '/' . $previewImage; return $this->mediaDirectory->delete($this->mediaDirectory->getRelativePath($previewImagePath)); } return false; } /** * Upload and create preview image * * @param string $scope the request key for file * @return $this */ public function uploadPreviewImage($scope) { $tmpDirPath = $this->themeImagePath->getTemporaryDirectory(); $tmpFilePath = $this->uploader->uploadPreviewImage($scope, $tmpDirPath); if ($tmpFilePath) { if ($this->theme->getPreviewImage()) { $this->removePreviewImage(); } $this->createPreviewImage($tmpFilePath); $this->mediaDirectory->delete($tmpFilePath); } return $this; } /** * Get path to preview image * * @return string */ public function getPreviewImagePath() { return $this->themeImagePath->getPreviewImagePath($this->theme); } /** * Get url of theme preview image * * @return string */ public function getPreviewImageUrl() { $previewImage = $this->theme->getPreviewImage(); return empty($previewImage) ? $this->themeImagePath->getPreviewImageDefaultUrl() : $this->themeImagePath->getPreviewImageUrl($this->theme); } }