24
24
use Magento \Framework \App \Filesystem \DirectoryList ;
25
25
26
26
/**
27
+ * Image resize service.
28
+ *
27
29
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28
30
*/
29
31
class ImageResize
@@ -123,7 +125,8 @@ public function __construct(
123
125
}
124
126
125
127
/**
126
- * Create resized images of different sizes from an original image
128
+ * Create resized images of different sizes from an original image.
129
+ *
127
130
* @param string $originalImageName
128
131
* @throws NotFoundException
129
132
*/
@@ -141,7 +144,8 @@ public function resizeFromImageName(string $originalImageName)
141
144
}
142
145
143
146
/**
144
- * Create resized images of different sizes from themes
147
+ * Create resized images of different sizes from themes.
148
+ *
145
149
* @param array|null $themes
146
150
* @return \Generator
147
151
* @throws NotFoundException
@@ -169,7 +173,8 @@ public function resizeFromThemes(array $themes = null): \Generator
169
173
}
170
174
171
175
/**
172
- * Search the current theme
176
+ * Search the current theme.
177
+ *
173
178
* @return array
174
179
*/
175
180
private function getThemesInUse (): array
@@ -187,7 +192,8 @@ private function getThemesInUse(): array
187
192
}
188
193
189
194
/**
190
- * Get view images data from themes
195
+ * Get view images data from themes.
196
+ *
191
197
* @param array $themes
192
198
* @return array
193
199
*/
@@ -211,7 +217,8 @@ private function getViewImages(array $themes): array
211
217
}
212
218
213
219
/**
214
- * Get unique image index
220
+ * Get unique image index.
221
+ *
215
222
* @param array $imageData
216
223
* @return string
217
224
*/
@@ -223,7 +230,8 @@ private function getUniqueImageIndex(array $imageData): string
223
230
}
224
231
225
232
/**
226
- * Make image
233
+ * Make image.
234
+ *
227
235
* @param string $originalImagePath
228
236
* @param array $imageParams
229
237
* @return Image
@@ -241,7 +249,8 @@ private function makeImage(string $originalImagePath, array $imageParams): Image
241
249
}
242
250
243
251
/**
244
- * Resize image
252
+ * Resize image.
253
+ *
245
254
* @param array $viewImage
246
255
* @param string $originalImagePath
247
256
* @param string $originalImageName
@@ -257,9 +266,41 @@ private function resize(array $viewImage, string $originalImagePath, string $ori
257
266
]
258
267
);
259
268
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
+
260
289
if ($ imageParams ['image_width ' ] !== null && $ imageParams ['image_height ' ] !== null ) {
261
290
$ image ->resize ($ imageParams ['image_width ' ], $ imageParams ['image_height ' ]);
262
291
}
263
292
$ image ->save ($ imageAsset ->getPath ());
264
293
}
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
+ }
265
306
}
0 commit comments