Image.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Design\Theme;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem\Directory\WriteInterface;
  9. use Magento\Framework\View\Design\ThemeInterface;
  10. /**
  11. * Theme Image model class
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class Image
  16. {
  17. /**
  18. * Preview image width
  19. */
  20. const PREVIEW_IMAGE_WIDTH = 800;
  21. /**
  22. * Preview image height
  23. */
  24. const PREVIEW_IMAGE_HEIGHT = 800;
  25. /**
  26. * Media directory
  27. *
  28. * @var WriteInterface
  29. */
  30. protected $mediaDirectory;
  31. /**
  32. * Root directory
  33. *
  34. * @var WriteInterface
  35. */
  36. protected $rootDirectory;
  37. /**
  38. * Image factory
  39. *
  40. * @var \Magento\Framework\Image\Factory
  41. */
  42. protected $imageFactory;
  43. /**
  44. * Image uploader
  45. *
  46. * @var Image\Uploader
  47. */
  48. protected $uploader;
  49. /**
  50. * Theme image path
  51. *
  52. * @var Image\PathInterface
  53. */
  54. protected $themeImagePath;
  55. /**
  56. * Logger
  57. *
  58. * @var \Psr\Log\LoggerInterface
  59. */
  60. protected $logger;
  61. /**
  62. * Theme
  63. *
  64. * @var ThemeInterface
  65. */
  66. protected $theme;
  67. /**
  68. * Width and height of preview image
  69. *
  70. * @var array
  71. */
  72. protected $imageParams;
  73. /**
  74. * Initialize dependencies
  75. *
  76. * @param \Magento\Framework\Filesystem $filesystem
  77. * @param \Magento\Framework\Image\Factory $imageFactory
  78. * @param Image\Uploader $uploader
  79. * @param Image\PathInterface $themeImagePath
  80. * @param \Psr\Log\LoggerInterface $logger
  81. * @param array $imageParams
  82. * @param ThemeInterface $theme
  83. * @codingStandardsIgnoreStart
  84. */
  85. public function __construct(
  86. \Magento\Framework\Filesystem $filesystem,
  87. \Magento\Framework\Image\Factory $imageFactory,
  88. Image\Uploader $uploader,
  89. Image\PathInterface $themeImagePath,
  90. \Psr\Log\LoggerInterface $logger,
  91. array $imageParams = [self::PREVIEW_IMAGE_WIDTH, self::PREVIEW_IMAGE_HEIGHT],
  92. ThemeInterface $theme = null
  93. ) {
  94. $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  95. $this->rootDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
  96. $this->imageFactory = $imageFactory;
  97. $this->uploader = $uploader;
  98. $this->themeImagePath = $themeImagePath;
  99. $this->logger = $logger;
  100. $this->imageParams = $imageParams;
  101. $this->theme = $theme;
  102. }
  103. // @codingStandardsIgnoreEnd
  104. /**
  105. * Create preview image
  106. *
  107. * @param string $imagePath
  108. * @return $this
  109. */
  110. public function createPreviewImage($imagePath)
  111. {
  112. list($imageWidth, $imageHeight) = $this->imageParams;
  113. $image = $this->imageFactory->create($imagePath);
  114. $image->keepTransparency(true);
  115. $image->constrainOnly(true);
  116. $image->keepFrame(true);
  117. $image->keepAspectRatio(true);
  118. $image->backgroundColor([255, 255, 255]);
  119. $image->resize($imageWidth, $imageHeight);
  120. $imageName = uniqid('preview_image_') . image_type_to_extension($image->getImageType());
  121. $image->save($this->themeImagePath->getImagePreviewDirectory(), $imageName);
  122. $this->theme->setPreviewImage($imageName);
  123. return $this;
  124. }
  125. /**
  126. * Create preview image duplicate
  127. *
  128. * @param ThemeInterface $theme
  129. * @return bool
  130. */
  131. public function createPreviewImageCopy(ThemeInterface $theme)
  132. {
  133. $previewDir = $this->themeImagePath->getImagePreviewDirectory();
  134. $sourcePath = $theme->getThemeImage()->getPreviewImagePath();
  135. $sourceRelativePath = $this->rootDirectory->getRelativePath($sourcePath);
  136. if (!$theme->getPreviewImage() && !$this->mediaDirectory->isExist($sourceRelativePath)) {
  137. return false;
  138. }
  139. $isCopied = false;
  140. try {
  141. $destinationFileName = \Magento\Framework\File\Uploader::getNewFileName($sourcePath);
  142. $targetRelativePath = $this->mediaDirectory->getRelativePath($previewDir . '/' . $destinationFileName);
  143. $isCopied = $this->rootDirectory->copyFile($sourceRelativePath, $targetRelativePath, $this->mediaDirectory);
  144. $this->theme->setPreviewImage($destinationFileName);
  145. } catch (\Magento\Framework\Exception\FileSystemException $e) {
  146. $this->theme->setPreviewImage(null);
  147. $this->logger->critical($e);
  148. }
  149. return $isCopied;
  150. }
  151. /**
  152. * Delete preview image
  153. *
  154. * @return bool
  155. */
  156. public function removePreviewImage()
  157. {
  158. $previewImage = $this->theme->getPreviewImage();
  159. $this->theme->setPreviewImage(null);
  160. if ($previewImage) {
  161. $previewImagePath = $this->themeImagePath->getImagePreviewDirectory() . '/' . $previewImage;
  162. return $this->mediaDirectory->delete($this->mediaDirectory->getRelativePath($previewImagePath));
  163. }
  164. return false;
  165. }
  166. /**
  167. * Upload and create preview image
  168. *
  169. * @param string $scope the request key for file
  170. * @return $this
  171. */
  172. public function uploadPreviewImage($scope)
  173. {
  174. $tmpDirPath = $this->themeImagePath->getTemporaryDirectory();
  175. $tmpFilePath = $this->uploader->uploadPreviewImage($scope, $tmpDirPath);
  176. if ($tmpFilePath) {
  177. if ($this->theme->getPreviewImage()) {
  178. $this->removePreviewImage();
  179. }
  180. $this->createPreviewImage($tmpFilePath);
  181. $this->mediaDirectory->delete($tmpFilePath);
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Get path to preview image
  187. *
  188. * @return string
  189. */
  190. public function getPreviewImagePath()
  191. {
  192. return $this->themeImagePath->getPreviewImagePath($this->theme);
  193. }
  194. /**
  195. * Get url of theme preview image
  196. *
  197. * @return string
  198. */
  199. public function getPreviewImageUrl()
  200. {
  201. $previewImage = $this->theme->getPreviewImage();
  202. return empty($previewImage)
  203. ? $this->themeImagePath->getPreviewImageDefaultUrl()
  204. : $this->themeImagePath->getPreviewImageUrl($this->theme);
  205. }
  206. }