Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Image.php
Go to the documentation of this file.
1 <?php
7 
11 
17 class Image
18 {
22  const PREVIEW_IMAGE_WIDTH = 800;
23 
27  const PREVIEW_IMAGE_HEIGHT = 800;
28 
34  protected $mediaDirectory;
35 
41  protected $rootDirectory;
42 
48  protected $imageFactory;
49 
55  protected $uploader;
56 
62  protected $themeImagePath;
63 
69  protected $logger;
70 
76  protected $theme;
77 
83  protected $imageParams;
84 
97  public function __construct(
98  \Magento\Framework\Filesystem $filesystem,
100  Image\Uploader $uploader,
101  Image\PathInterface $themeImagePath,
102  \Psr\Log\LoggerInterface $logger,
103  array $imageParams = [self::PREVIEW_IMAGE_WIDTH, self::PREVIEW_IMAGE_HEIGHT],
104  ThemeInterface $theme = null
105  ) {
106  $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
107  $this->rootDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
108  $this->imageFactory = $imageFactory;
109  $this->uploader = $uploader;
110  $this->themeImagePath = $themeImagePath;
111  $this->logger = $logger;
112  $this->imageParams = $imageParams;
113  $this->theme = $theme;
114  }
115 
116  // @codingStandardsIgnoreEnd
117 
124  public function createPreviewImage($imagePath)
125  {
126  list($imageWidth, $imageHeight) = $this->imageParams;
127  $image = $this->imageFactory->create($imagePath);
128  $image->keepTransparency(true);
129  $image->constrainOnly(true);
130  $image->keepFrame(true);
131  $image->keepAspectRatio(true);
132  $image->backgroundColor([255, 255, 255]);
133  $image->resize($imageWidth, $imageHeight);
134 
135  $imageName = uniqid('preview_image_') . image_type_to_extension($image->getImageType());
136  $image->save($this->themeImagePath->getImagePreviewDirectory(), $imageName);
137  $this->theme->setPreviewImage($imageName);
138  return $this;
139  }
140 
148  {
149  $previewDir = $this->themeImagePath->getImagePreviewDirectory();
150  $sourcePath = $theme->getThemeImage()->getPreviewImagePath();
151  $sourceRelativePath = $this->rootDirectory->getRelativePath($sourcePath);
152  if (!$theme->getPreviewImage() && !$this->mediaDirectory->isExist($sourceRelativePath)) {
153  return false;
154  }
155  $isCopied = false;
156  try {
157  $destinationFileName = \Magento\Framework\File\Uploader::getNewFileName($sourcePath);
158  $targetRelativePath = $this->mediaDirectory->getRelativePath($previewDir . '/' . $destinationFileName);
159  $isCopied = $this->rootDirectory->copyFile($sourceRelativePath, $targetRelativePath, $this->mediaDirectory);
160  $this->theme->setPreviewImage($destinationFileName);
161  } catch (\Magento\Framework\Exception\FileSystemException $e) {
162  $this->theme->setPreviewImage(null);
163  $this->logger->critical($e);
164  }
165  return $isCopied;
166  }
167 
173  public function removePreviewImage()
174  {
175  $previewImage = $this->theme->getPreviewImage();
176  $this->theme->setPreviewImage(null);
177  if ($previewImage) {
178  $previewImagePath = $this->themeImagePath->getImagePreviewDirectory() . '/' . $previewImage;
179  return $this->mediaDirectory->delete($this->mediaDirectory->getRelativePath($previewImagePath));
180  }
181  return false;
182  }
183 
190  public function uploadPreviewImage($scope)
191  {
192  $tmpDirPath = $this->themeImagePath->getTemporaryDirectory();
193  $tmpFilePath = $this->uploader->uploadPreviewImage($scope, $tmpDirPath);
194  if ($tmpFilePath) {
195  if ($this->theme->getPreviewImage()) {
196  $this->removePreviewImage();
197  }
198  $this->createPreviewImage($tmpFilePath);
199  $this->mediaDirectory->delete($tmpFilePath);
200  }
201  return $this;
202  }
203 
209  public function getPreviewImagePath()
210  {
211  return $this->themeImagePath->getPreviewImagePath($this->theme);
212  }
213 
219  public function getPreviewImageUrl()
220  {
221  $previewImage = $this->theme->getPreviewImage();
222  return empty($previewImage)
223  ? $this->themeImagePath->getPreviewImageDefaultUrl()
224  : $this->themeImagePath->getPreviewImageUrl($this->theme);
225  }
226 }
createPreviewImageCopy(ThemeInterface $theme)
Definition: Image.php:147
__construct(\Magento\Framework\Filesystem $filesystem, \Magento\Framework\Image\Factory $imageFactory, Image\Uploader $uploader, Image\PathInterface $themeImagePath, \Psr\Log\LoggerInterface $logger, array $imageParams=[self::PREVIEW_IMAGE_WIDTH, self::PREVIEW_IMAGE_HEIGHT], ThemeInterface $theme=null)
Definition: Image.php:97
$filesystem
static getNewFileName($destinationFile)
Definition: Uploader.php:605