-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathImageResize.php
413 lines (370 loc) · 13.3 KB
/
ImageResize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MediaStorage\Service;
use Generator;
use Magento\Catalog\Helper\Image as ImageHelper;
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
use Magento\Catalog\Model\View\Asset\ImageFactory as AssertImageFactory;
use Magento\Framework\App\Area;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\Image;
use Magento\Framework\Image\Factory as ImageFactory;
use Magento\Catalog\Model\Product\Media\ConfigInterface as MediaConfig;
use Magento\Framework\App\State;
use Magento\Framework\View\ConfigInterface as ViewConfig;
use Magento\Catalog\Model\ResourceModel\Product\Image as ProductImage;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Theme\Model\Config\Customization as ThemeCustomizationConfig;
use Magento\Theme\Model\ResourceModel\Theme\Collection as ThemeCollection;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\MediaStorage\Helper\File\Storage\Database as FileStorageDatabase;
use Magento\Theme\Model\Theme;
/**
* Image resize service.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ImageResize
{
/**
* @var State
*/
private $appState;
/**
* @var MediaConfig
*/
private $imageConfig;
/**
* @var ProductImage
*/
private $productImage;
/**
* @var ImageFactory
*/
private $imageFactory;
/**
* @var ParamsBuilder
*/
private $paramsBuilder;
/**
* @var ViewConfig
*/
private $viewConfig;
/**
* @var AssertImageFactory
*/
private $assertImageFactory;
/**
* @var ThemeCustomizationConfig
*/
private $themeCustomizationConfig;
/**
* @var ThemeCollection
*/
private $themeCollection;
/**
* @var WriteInterface
*/
private $mediaDirectory;
/**
* @var FileStorageDatabase
*/
private $fileStorageDatabase;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @param State $appState
* @param MediaConfig $imageConfig
* @param ProductImage $productImage
* @param ImageFactory $imageFactory
* @param ParamsBuilder $paramsBuilder
* @param ViewConfig $viewConfig
* @param AssertImageFactory $assertImageFactory
* @param ThemeCustomizationConfig $themeCustomizationConfig
* @param ThemeCollection $themeCollection
* @param Filesystem $filesystem
* @param FileStorageDatabase $fileStorageDatabase
* @param StoreManagerInterface $storeManager
* @throws \Magento\Framework\Exception\FileSystemException
* @internal param ProductImage $gallery
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
State $appState,
MediaConfig $imageConfig,
ProductImage $productImage,
ImageFactory $imageFactory,
ParamsBuilder $paramsBuilder,
ViewConfig $viewConfig,
AssertImageFactory $assertImageFactory,
ThemeCustomizationConfig $themeCustomizationConfig,
ThemeCollection $themeCollection,
Filesystem $filesystem,
?FileStorageDatabase $fileStorageDatabase = null,
?StoreManagerInterface $storeManager = null
) {
$this->appState = $appState;
$this->imageConfig = $imageConfig;
$this->productImage = $productImage;
$this->imageFactory = $imageFactory;
$this->paramsBuilder = $paramsBuilder;
$this->viewConfig = $viewConfig;
$this->assertImageFactory = $assertImageFactory;
$this->themeCustomizationConfig = $themeCustomizationConfig;
$this->themeCollection = $themeCollection;
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->fileStorageDatabase = $fileStorageDatabase ?:
ObjectManager::getInstance()->get(FileStorageDatabase::class);
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
}
/**
* Create resized images of different sizes from an original image.
*
* @param string $originalImageName
* @throws NotFoundException
*/
public function resizeFromImageName(string $originalImageName)
{
$mediastoragefilename = $this->imageConfig->getMediaPath($originalImageName);
$originalImagePath = $this->mediaDirectory->getAbsolutePath($mediastoragefilename);
if ($this->fileStorageDatabase->checkDbUsage() &&
!$this->mediaDirectory->isFile($mediastoragefilename)
) {
$this->fileStorageDatabase->saveFileToFilesystem($mediastoragefilename);
}
if (!$this->mediaDirectory->isFile($originalImagePath)) {
throw new NotFoundException(__('Cannot resize image "%1" - original image not found', $originalImagePath));
}
foreach ($this->getViewImages($this->getThemesInUse()) as $viewImage) {
$this->resize($viewImage, $originalImagePath, $originalImageName);
}
}
/**
* Create resized images of different sizes from themes.
*
* @param array|null $themes
* @param bool $skipHiddenImages
* @return Generator
* @throws NotFoundException
*/
public function resizeFromThemes(?array $themes = null, bool $skipHiddenImages = false): Generator
{
$count = $this->getCountProductImages($skipHiddenImages);
if (!$count) {
throw new NotFoundException(__('Cannot resize images - product images not found'));
}
$productImages = $this->getProductImages($skipHiddenImages);
$viewImages = $this->getViewImages($themes ?? $this->getThemesInUse());
foreach ($productImages as $image) {
$error = '';
$originalImageName = $image['filepath'];
$mediastoragefilename = $this->imageConfig->getMediaPath($originalImageName);
$originalImagePath = $this->mediaDirectory->getAbsolutePath($mediastoragefilename);
if ($this->fileStorageDatabase->checkDbUsage()) {
$this->fileStorageDatabase->saveFileToFilesystem($mediastoragefilename);
}
if ($this->mediaDirectory->isFile($originalImagePath)) {
try {
foreach ($viewImages as $viewImage) {
$this->resize($viewImage, $originalImagePath, $originalImageName);
}
} catch (\Exception $e) {
$error = $e->getMessage();
}
} else {
$error = __('Cannot resize image "%1" - original image not found', $originalImagePath);
}
yield ['filename' => $originalImageName, 'error' => (string) $error] => $count;
}
}
/**
* @param bool $skipHiddenImages
* @return int
*/
public function getCountProductImages(bool $skipHiddenImages = false): int
{
return $skipHiddenImages ?
$this->productImage->getCountUsedProductImages() : $this->productImage->getCountAllProductImages();
}
/**
* @param bool $skipHiddenImages
* @return Generator
*/
public function getProductImages(bool $skipHiddenImages = false): \Generator
{
return $skipHiddenImages ?
$this->productImage->getUsedProductImages() : $this->productImage->getAllProductImages();
}
/**
* Search the current theme.
*
* @return array
*/
private function getThemesInUse(): array
{
$themesInUse = [];
$registeredThemes = $this->themeCollection->loadRegisteredThemes();
$storesByThemes = $this->themeCustomizationConfig->getStoresByThemes();
$keyType = is_integer(key($storesByThemes)) ? 'getId' : 'getCode';
foreach ($registeredThemes as $registeredTheme) {
if (array_key_exists($registeredTheme->$keyType(), $storesByThemes)) {
$themesInUse[] = $registeredTheme;
}
}
return $themesInUse;
}
/**
* Get view images data from themes.
*
* @param array $themes
* @return array
*/
private function getViewImages(array $themes): array
{
$viewImages = [];
$stores = $this->storeManager->getStores(true);
/** @var Theme $theme */
foreach ($themes as $theme) {
$config = $this->viewConfig->getViewConfig(
[
'area' => Area::AREA_FRONTEND,
'themeModel' => $theme,
]
);
$images = $config->getMediaEntities('Magento_Catalog', ImageHelper::MEDIA_TYPE_CONFIG_NODE);
foreach ($images as $imageId => $imageData) {
foreach ($stores as $store) {
$data = $this->paramsBuilder->build($imageData, (int) $store->getId());
$uniqIndex = $this->getUniqueImageIndex($data);
$data['id'] = $imageId;
$viewImages[$uniqIndex] = $data;
}
}
}
return $viewImages;
}
/**
* Get unique image index.
*
* @param array $imageData
* @return string
*/
private function getUniqueImageIndex(array $imageData): string
{
ksort($imageData);
unset($imageData['type']);
// phpcs:disable Magento2.Security.InsecureFunction
return md5(json_encode($imageData));
}
/**
* Make image.
*
* @param string $originalImagePath
* @param array $imageParams
* @return Image
* @throws \InvalidArgumentException
*/
private function makeImage(string $originalImagePath, array $imageParams): Image
{
$image = $this->imageFactory->create($originalImagePath);
$image->keepAspectRatio($imageParams['keep_aspect_ratio']);
$image->keepFrame($imageParams['keep_frame']);
$image->keepTransparency($imageParams['keep_transparency']);
$image->constrainOnly($imageParams['constrain_only']);
$image->backgroundColor($imageParams['background']);
$image->quality($imageParams['quality']);
return $image;
}
/**
* Resize image if not already resized before
*
* @param array $imageParams
* @param string $originalImagePath
* @param string $originalImageName
*/
private function resize(array $imageParams, string $originalImagePath, string $originalImageName)
{
unset($imageParams['id']);
$imageAsset = $this->assertImageFactory->create(
[
'miscParams' => $imageParams,
'filePath' => $originalImageName,
]
);
$imageAssetPath = $imageAsset->getPath();
$usingDbAsStorage = $this->fileStorageDatabase->checkDbUsage();
$mediaStorageFilename = $this->mediaDirectory->getRelativePath($imageAssetPath);
$alreadyResized = $usingDbAsStorage ?
$this->fileStorageDatabase->fileExists($mediaStorageFilename) :
$this->mediaDirectory->isFile($imageAssetPath);
if (!$alreadyResized) {
$this->generateResizedImage(
$imageParams,
$originalImagePath,
$imageAssetPath,
$usingDbAsStorage,
$mediaStorageFilename
);
}
}
/**
* Generate resized image
*
* @param array $imageParams
* @param string $originalImagePath
* @param string $imageAssetPath
* @param bool $usingDbAsStorage
* @param string $mediaStorageFilename
*/
private function generateResizedImage(
array $imageParams,
string $originalImagePath,
string $imageAssetPath,
bool $usingDbAsStorage,
string $mediaStorageFilename
) {
$image = $this->makeImage($originalImagePath, $imageParams);
if ($imageParams['image_width'] !== null && $imageParams['image_height'] !== null) {
$image->resize($imageParams['image_width'], $imageParams['image_height']);
}
if (isset($imageParams['watermark_file'])) {
if ($imageParams['watermark_height'] !== null) {
$image->setWatermarkHeight($imageParams['watermark_height']);
}
if ($imageParams['watermark_width'] !== null) {
$image->setWatermarkWidth($imageParams['watermark_width']);
}
if ($imageParams['watermark_position'] !== null) {
$image->setWatermarkPosition($imageParams['watermark_position']);
}
if ($imageParams['watermark_image_opacity'] !== null) {
$image->setWatermarkImageOpacity($imageParams['watermark_image_opacity']);
}
$image->watermark($this->getWatermarkFilePath($imageParams['watermark_file']));
}
$image->save($imageAssetPath);
if ($usingDbAsStorage) {
$this->fileStorageDatabase->saveFile($mediaStorageFilename);
}
}
/**
* Returns watermark file absolute path
*
* @param string $file
* @return string
*/
private function getWatermarkFilePath($file)
{
$path = $this->imageConfig->getMediaPath('/watermark/' . $file);
return $this->mediaDirectory->getAbsolutePath($path);
}
}