Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImageResize.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Magento\Catalog\Helper\Image as ImageHelper;
12 use Magento\Catalog\Model\View\Asset\ImageFactory as AssertImageFactory;
17 use Magento\Framework\Image\Factory as ImageFactory;
21 use \Magento\Catalog\Model\ResourceModel\Product\Image as ProductImage;
22 use Magento\Theme\Model\Config\Customization as ThemeCustomizationConfig;
25 
30 {
34  private $appState;
35 
39  private $imageConfig;
40 
44  private $productImage;
45 
49  private $imageFactory;
50 
54  private $paramsBuilder;
55 
59  private $viewConfig;
60 
64  private $assertImageFactory;
65 
69  private $themeCustomizationConfig;
70 
74  private $themeCollection;
75 
79  private $mediaDirectory;
80 
84  private $filesystem;
85 
100  public function __construct(
101  State $appState,
102  MediaConfig $imageConfig,
103  ProductImage $productImage,
104  ImageFactory $imageFactory,
105  ParamsBuilder $paramsBuilder,
106  ViewConfig $viewConfig,
107  AssertImageFactory $assertImageFactory,
108  ThemeCustomizationConfig $themeCustomizationConfig,
109  Collection $themeCollection,
110  Filesystem $filesystem
111  ) {
112  $this->appState = $appState;
113  $this->imageConfig = $imageConfig;
114  $this->productImage = $productImage;
115  $this->imageFactory = $imageFactory;
116  $this->paramsBuilder = $paramsBuilder;
117  $this->viewConfig = $viewConfig;
118  $this->assertImageFactory = $assertImageFactory;
119  $this->themeCustomizationConfig = $themeCustomizationConfig;
120  $this->themeCollection = $themeCollection;
121  $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
122  $this->filesystem = $filesystem;
123  }
124 
130  public function resizeFromImageName(string $originalImageName)
131  {
132  $originalImagePath = $this->mediaDirectory->getAbsolutePath(
133  $this->imageConfig->getMediaPath($originalImageName)
134  );
135  if (!$this->mediaDirectory->isFile($originalImagePath)) {
136  throw new NotFoundException(__('Cannot resize image "%1" - original image not found', $originalImagePath));
137  }
138  foreach ($this->getViewImages($this->getThemesInUse()) as $viewImage) {
139  $this->resize($viewImage, $originalImagePath, $originalImageName);
140  }
141  }
142 
149  public function resizeFromThemes(array $themes = null): \Generator
150  {
151  $count = $this->productImage->getCountAllProductImages();
152  if (!$count) {
153  throw new NotFoundException(__('Cannot resize images - product images not found'));
154  }
155 
156  $productImages = $this->productImage->getAllProductImages();
157  $viewImages = $this->getViewImages($themes ?? $this->getThemesInUse());
158 
159  foreach ($productImages as $image) {
160  $originalImageName = $image['filepath'];
161  $originalImagePath = $this->mediaDirectory->getAbsolutePath(
162  $this->imageConfig->getMediaPath($originalImageName)
163  );
164  foreach ($viewImages as $viewImage) {
165  $this->resize($viewImage, $originalImagePath, $originalImageName);
166  }
167  yield $originalImageName => $count;
168  }
169  }
170 
175  private function getThemesInUse(): array
176  {
177  $themesInUse = [];
178  $registeredThemes = $this->themeCollection->loadRegisteredThemes();
179  $storesByThemes = $this->themeCustomizationConfig->getStoresByThemes();
180  $keyType = is_integer(key($storesByThemes)) ? 'getId' : 'getCode';
181  foreach ($registeredThemes as $registeredTheme) {
182  if (array_key_exists($registeredTheme->$keyType(), $storesByThemes)) {
183  $themesInUse[] = $registeredTheme;
184  }
185  }
186  return $themesInUse;
187  }
188 
194  private function getViewImages(array $themes): array
195  {
196  $viewImages = [];
198  foreach ($themes as $theme) {
199  $config = $this->viewConfig->getViewConfig([
200  'area' => Area::AREA_FRONTEND,
201  'themeModel' => $theme,
202  ]);
203  $images = $config->getMediaEntities('Magento_Catalog', ImageHelper::MEDIA_TYPE_CONFIG_NODE);
204  foreach ($images as $imageId => $imageData) {
205  $uniqIndex = $this->getUniqueImageIndex($imageData);
206  $imageData['id'] = $imageId;
207  $viewImages[$uniqIndex] = $imageData;
208  }
209  }
210  return $viewImages;
211  }
212 
218  private function getUniqueImageIndex(array $imageData): string
219  {
220  ksort($imageData);
221  unset($imageData['type']);
222  return md5(json_encode($imageData));
223  }
224 
231  private function makeImage(string $originalImagePath, array $imageParams): Image
232  {
233  $image = $this->imageFactory->create($originalImagePath);
234  $image->keepAspectRatio($imageParams['keep_aspect_ratio']);
235  $image->keepFrame($imageParams['keep_frame']);
236  $image->keepTransparency($imageParams['keep_transparency']);
237  $image->constrainOnly($imageParams['constrain_only']);
238  $image->backgroundColor($imageParams['background']);
239  $image->quality($imageParams['quality']);
240  return $image;
241  }
242 
249  private function resize(array $viewImage, string $originalImagePath, string $originalImageName)
250  {
251  $imageParams = $this->paramsBuilder->build($viewImage);
252  $image = $this->makeImage($originalImagePath, $imageParams);
253  $imageAsset = $this->assertImageFactory->create(
254  [
255  'miscParams' => $imageParams,
256  'filePath' => $originalImageName,
257  ]
258  );
259 
260  if ($imageParams['image_width'] !== null && $imageParams['image_height'] !== null) {
261  $image->resize($imageParams['image_width'], $imageParams['image_height']);
262  }
263  $image->save($imageAsset->getPath());
264  }
265 }
$config
Definition: fraud_order.php:17
$count
Definition: recent.phtml:13
__()
Definition: __.php:13
resizeFromImageName(string $originalImageName)
$theme
__construct(State $appState, MediaConfig $imageConfig, ProductImage $productImage, ImageFactory $imageFactory, ParamsBuilder $paramsBuilder, ViewConfig $viewConfig, AssertImageFactory $assertImageFactory, ThemeCustomizationConfig $themeCustomizationConfig, Collection $themeCollection, Filesystem $filesystem)
$filesystem