ImageResize.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\MediaStorage\Service;
  8. use Magento\Catalog\Helper\Image as ImageHelper;
  9. use Magento\Catalog\Model\Product\Image\ParamsBuilder;
  10. use Magento\Catalog\Model\View\Asset\ImageFactory as AssertImageFactory;
  11. use Magento\Framework\App\Area;
  12. use Magento\Framework\Exception\NotFoundException;
  13. use Magento\Framework\Filesystem;
  14. use Magento\Framework\Image;
  15. use Magento\Framework\Image\Factory as ImageFactory;
  16. use Magento\Catalog\Model\Product\Media\ConfigInterface as MediaConfig;
  17. use Magento\Framework\App\State;
  18. use Magento\Framework\View\ConfigInterface as ViewConfig;
  19. use \Magento\Catalog\Model\ResourceModel\Product\Image as ProductImage;
  20. use Magento\Theme\Model\Config\Customization as ThemeCustomizationConfig;
  21. use Magento\Theme\Model\ResourceModel\Theme\Collection;
  22. use Magento\Framework\App\Filesystem\DirectoryList;
  23. /**
  24. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25. */
  26. class ImageResize
  27. {
  28. /**
  29. * @var State
  30. */
  31. private $appState;
  32. /**
  33. * @var MediaConfig
  34. */
  35. private $imageConfig;
  36. /**
  37. * @var ProductImage
  38. */
  39. private $productImage;
  40. /**
  41. * @var ImageFactory
  42. */
  43. private $imageFactory;
  44. /**
  45. * @var ParamsBuilder
  46. */
  47. private $paramsBuilder;
  48. /**
  49. * @var ViewConfig
  50. */
  51. private $viewConfig;
  52. /**
  53. * @var AssertImageFactory
  54. */
  55. private $assertImageFactory;
  56. /**
  57. * @var ThemeCustomizationConfig
  58. */
  59. private $themeCustomizationConfig;
  60. /**
  61. * @var Collection
  62. */
  63. private $themeCollection;
  64. /**
  65. * @var Filesystem
  66. */
  67. private $mediaDirectory;
  68. /**
  69. * @var Filesystem
  70. */
  71. private $filesystem;
  72. /**
  73. * @param State $appState
  74. * @param MediaConfig $imageConfig
  75. * @param ProductImage $productImage
  76. * @param ImageFactory $imageFactory
  77. * @param ParamsBuilder $paramsBuilder
  78. * @param ViewConfig $viewConfig
  79. * @param AssertImageFactory $assertImageFactory
  80. * @param ThemeCustomizationConfig $themeCustomizationConfig
  81. * @param Collection $themeCollection
  82. * @param Filesystem $filesystem
  83. * @internal param ProductImage $gallery
  84. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  85. */
  86. public function __construct(
  87. State $appState,
  88. MediaConfig $imageConfig,
  89. ProductImage $productImage,
  90. ImageFactory $imageFactory,
  91. ParamsBuilder $paramsBuilder,
  92. ViewConfig $viewConfig,
  93. AssertImageFactory $assertImageFactory,
  94. ThemeCustomizationConfig $themeCustomizationConfig,
  95. Collection $themeCollection,
  96. Filesystem $filesystem
  97. ) {
  98. $this->appState = $appState;
  99. $this->imageConfig = $imageConfig;
  100. $this->productImage = $productImage;
  101. $this->imageFactory = $imageFactory;
  102. $this->paramsBuilder = $paramsBuilder;
  103. $this->viewConfig = $viewConfig;
  104. $this->assertImageFactory = $assertImageFactory;
  105. $this->themeCustomizationConfig = $themeCustomizationConfig;
  106. $this->themeCollection = $themeCollection;
  107. $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  108. $this->filesystem = $filesystem;
  109. }
  110. /**
  111. * Create resized images of different sizes from an original image
  112. * @param string $originalImageName
  113. * @throws NotFoundException
  114. */
  115. public function resizeFromImageName(string $originalImageName)
  116. {
  117. $originalImagePath = $this->mediaDirectory->getAbsolutePath(
  118. $this->imageConfig->getMediaPath($originalImageName)
  119. );
  120. if (!$this->mediaDirectory->isFile($originalImagePath)) {
  121. throw new NotFoundException(__('Cannot resize image "%1" - original image not found', $originalImagePath));
  122. }
  123. foreach ($this->getViewImages($this->getThemesInUse()) as $viewImage) {
  124. $this->resize($viewImage, $originalImagePath, $originalImageName);
  125. }
  126. }
  127. /**
  128. * Create resized images of different sizes from themes
  129. * @param array|null $themes
  130. * @return \Generator
  131. * @throws NotFoundException
  132. */
  133. public function resizeFromThemes(array $themes = null): \Generator
  134. {
  135. $count = $this->productImage->getCountAllProductImages();
  136. if (!$count) {
  137. throw new NotFoundException(__('Cannot resize images - product images not found'));
  138. }
  139. $productImages = $this->productImage->getAllProductImages();
  140. $viewImages = $this->getViewImages($themes ?? $this->getThemesInUse());
  141. foreach ($productImages as $image) {
  142. $originalImageName = $image['filepath'];
  143. $originalImagePath = $this->mediaDirectory->getAbsolutePath(
  144. $this->imageConfig->getMediaPath($originalImageName)
  145. );
  146. foreach ($viewImages as $viewImage) {
  147. $this->resize($viewImage, $originalImagePath, $originalImageName);
  148. }
  149. yield $originalImageName => $count;
  150. }
  151. }
  152. /**
  153. * Search the current theme
  154. * @return array
  155. */
  156. private function getThemesInUse(): array
  157. {
  158. $themesInUse = [];
  159. $registeredThemes = $this->themeCollection->loadRegisteredThemes();
  160. $storesByThemes = $this->themeCustomizationConfig->getStoresByThemes();
  161. $keyType = is_integer(key($storesByThemes)) ? 'getId' : 'getCode';
  162. foreach ($registeredThemes as $registeredTheme) {
  163. if (array_key_exists($registeredTheme->$keyType(), $storesByThemes)) {
  164. $themesInUse[] = $registeredTheme;
  165. }
  166. }
  167. return $themesInUse;
  168. }
  169. /**
  170. * Get view images data from themes
  171. * @param array $themes
  172. * @return array
  173. */
  174. private function getViewImages(array $themes): array
  175. {
  176. $viewImages = [];
  177. /** @var \Magento\Theme\Model\Theme $theme */
  178. foreach ($themes as $theme) {
  179. $config = $this->viewConfig->getViewConfig([
  180. 'area' => Area::AREA_FRONTEND,
  181. 'themeModel' => $theme,
  182. ]);
  183. $images = $config->getMediaEntities('Magento_Catalog', ImageHelper::MEDIA_TYPE_CONFIG_NODE);
  184. foreach ($images as $imageId => $imageData) {
  185. $uniqIndex = $this->getUniqueImageIndex($imageData);
  186. $imageData['id'] = $imageId;
  187. $viewImages[$uniqIndex] = $imageData;
  188. }
  189. }
  190. return $viewImages;
  191. }
  192. /**
  193. * Get unique image index
  194. * @param array $imageData
  195. * @return string
  196. */
  197. private function getUniqueImageIndex(array $imageData): string
  198. {
  199. ksort($imageData);
  200. unset($imageData['type']);
  201. return md5(json_encode($imageData));
  202. }
  203. /**
  204. * Make image
  205. * @param string $originalImagePath
  206. * @param array $imageParams
  207. * @return Image
  208. */
  209. private function makeImage(string $originalImagePath, array $imageParams): Image
  210. {
  211. $image = $this->imageFactory->create($originalImagePath);
  212. $image->keepAspectRatio($imageParams['keep_aspect_ratio']);
  213. $image->keepFrame($imageParams['keep_frame']);
  214. $image->keepTransparency($imageParams['keep_transparency']);
  215. $image->constrainOnly($imageParams['constrain_only']);
  216. $image->backgroundColor($imageParams['background']);
  217. $image->quality($imageParams['quality']);
  218. return $image;
  219. }
  220. /**
  221. * Resize image
  222. * @param array $viewImage
  223. * @param string $originalImagePath
  224. * @param string $originalImageName
  225. */
  226. private function resize(array $viewImage, string $originalImagePath, string $originalImageName)
  227. {
  228. $imageParams = $this->paramsBuilder->build($viewImage);
  229. $image = $this->makeImage($originalImagePath, $imageParams);
  230. $imageAsset = $this->assertImageFactory->create(
  231. [
  232. 'miscParams' => $imageParams,
  233. 'filePath' => $originalImageName,
  234. ]
  235. );
  236. if ($imageParams['image_width'] !== null && $imageParams['image_height'] !== null) {
  237. $image->resize($imageParams['image_width'], $imageParams['image_height']);
  238. }
  239. $image->save($imageAsset->getPath());
  240. }
  241. }