Skip to content

Commit 9a78c5e

Browse files
authored
ENGCOM-4378: [Catalog] [MediaStorage] Fix watermark in media application #21338
2 parents b736175 + 13b3498 commit 9a78c5e

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

app/code/Magento/MediaStorage/Service/ImageResize.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Magento\Framework\App\Filesystem\DirectoryList;
2525

2626
/**
27+
* Image resize service.
28+
*
2729
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2830
*/
2931
class ImageResize
@@ -123,7 +125,8 @@ public function __construct(
123125
}
124126

125127
/**
126-
* Create resized images of different sizes from an original image
128+
* Create resized images of different sizes from an original image.
129+
*
127130
* @param string $originalImageName
128131
* @throws NotFoundException
129132
*/
@@ -141,7 +144,8 @@ public function resizeFromImageName(string $originalImageName)
141144
}
142145

143146
/**
144-
* Create resized images of different sizes from themes
147+
* Create resized images of different sizes from themes.
148+
*
145149
* @param array|null $themes
146150
* @return \Generator
147151
* @throws NotFoundException
@@ -169,7 +173,8 @@ public function resizeFromThemes(array $themes = null): \Generator
169173
}
170174

171175
/**
172-
* Search the current theme
176+
* Search the current theme.
177+
*
173178
* @return array
174179
*/
175180
private function getThemesInUse(): array
@@ -187,7 +192,8 @@ private function getThemesInUse(): array
187192
}
188193

189194
/**
190-
* Get view images data from themes
195+
* Get view images data from themes.
196+
*
191197
* @param array $themes
192198
* @return array
193199
*/
@@ -211,7 +217,8 @@ private function getViewImages(array $themes): array
211217
}
212218

213219
/**
214-
* Get unique image index
220+
* Get unique image index.
221+
*
215222
* @param array $imageData
216223
* @return string
217224
*/
@@ -223,7 +230,8 @@ private function getUniqueImageIndex(array $imageData): string
223230
}
224231

225232
/**
226-
* Make image
233+
* Make image.
234+
*
227235
* @param string $originalImagePath
228236
* @param array $imageParams
229237
* @return Image
@@ -241,7 +249,8 @@ private function makeImage(string $originalImagePath, array $imageParams): Image
241249
}
242250

243251
/**
244-
* Resize image
252+
* Resize image.
253+
*
245254
* @param array $viewImage
246255
* @param string $originalImagePath
247256
* @param string $originalImageName
@@ -257,9 +266,41 @@ private function resize(array $viewImage, string $originalImagePath, string $ori
257266
]
258267
);
259268

269+
if (isset($imageParams['watermark_file'])) {
270+
if ($imageParams['watermark_height'] !== null) {
271+
$image->setWatermarkHeight($imageParams['watermark_height']);
272+
}
273+
274+
if ($imageParams['watermark_width'] !== null) {
275+
$image->setWatermarkWidth($imageParams['watermark_width']);
276+
}
277+
278+
if ($imageParams['watermark_position'] !== null) {
279+
$image->setWatermarkPosition($imageParams['watermark_position']);
280+
}
281+
282+
if ($imageParams['watermark_image_opacity'] !== null) {
283+
$image->setWatermarkImageOpacity($imageParams['watermark_image_opacity']);
284+
}
285+
286+
$image->watermark($this->getWatermarkFilePath($imageParams['watermark_file']));
287+
}
288+
260289
if ($imageParams['image_width'] !== null && $imageParams['image_height'] !== null) {
261290
$image->resize($imageParams['image_width'], $imageParams['image_height']);
262291
}
263292
$image->save($imageAsset->getPath());
264293
}
294+
295+
/**
296+
* Returns watermark file absolute path
297+
*
298+
* @param string $file
299+
* @return string
300+
*/
301+
private function getWatermarkFilePath($file)
302+
{
303+
$path = $this->imageConfig->getMediaPath('/watermark/' . $file);
304+
return $this->mediaDirectory->getAbsolutePath($path);
305+
}
265306
}

0 commit comments

Comments
 (0)