@@ -61,9 +61,9 @@ protected function _reset()
61
61
*/
62
62
public function open ($ filename )
63
63
{
64
- if (! $ filename || !file_exists ($ filename )) {
64
+ if ($ filename === null || !file_exists ($ filename )) {
65
65
throw new FileSystemException (
66
- new Phrase ('File "%1" does not exist. ' , [$ this -> _fileName ])
66
+ new Phrase ('File "%1" does not exist. ' , [$ filename ])
67
67
);
68
68
}
69
69
if (!$ filename || filesize ($ filename ) === 0 || !$ this ->validateURLScheme ($ filename )) {
@@ -188,7 +188,7 @@ public function save($destination = null, $newName = null)
188
188
} else {
189
189
$ newImage = imagecreate ($ this ->_imageSrcWidth , $ this ->_imageSrcHeight );
190
190
}
191
- $ this ->_fillBackgroundColor ($ newImage );
191
+ $ this ->fillBackgroundColor ($ newImage );
192
192
imagecopy ($ newImage , $ this ->_imageHandler , 0 , 0 , 0 , 0 , $ this ->_imageSrcWidth , $ this ->_imageSrcHeight );
193
193
$ this ->imageDestroy ();
194
194
$ this ->_imageHandler = $ newImage ;
@@ -265,64 +265,98 @@ private function _getCallback($callbackType, $fileType = null, $unsupportedText
265
265
* Returns a color identifier.
266
266
*
267
267
* @param resource &$imageResourceTo
268
+ *
268
269
* @return void
269
270
* @throws \InvalidArgumentException
270
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
271
- * @SuppressWarnings(PHPMD.NPathComplexity)
272
271
*/
273
- private function _fillBackgroundColor (&$ imageResourceTo )
272
+ private function fillBackgroundColor (&$ imageResourceTo ): void
274
273
{
275
274
// try to keep transparency, if any
276
275
if ($ this ->_keepTransparency ) {
277
276
$ isAlpha = false ;
278
277
$ transparentIndex = $ this ->_getTransparency ($ this ->_imageHandler , $ this ->_fileType , $ isAlpha );
278
+
279
279
try {
280
- // fill truecolor png with alpha transparency
280
+ // fill true color png with alpha transparency
281
281
if ($ isAlpha ) {
282
- if (!imagealphablending ($ imageResourceTo , false )) {
283
- throw new \InvalidArgumentException ('Failed to set alpha blending for PNG image. ' );
284
- }
285
- $ transparentAlphaColor = imagecolorallocatealpha ($ imageResourceTo , 0 , 0 , 0 , 127 );
286
- if (false === $ transparentAlphaColor ) {
287
- throw new \InvalidArgumentException ('Failed to allocate alpha transparency for PNG image. ' );
288
- }
289
- if (!imagefill ($ imageResourceTo , 0 , 0 , $ transparentAlphaColor )) {
290
- throw new \InvalidArgumentException ('Failed to fill PNG image with alpha transparency. ' );
291
- }
292
- if (!imagesavealpha ($ imageResourceTo , true )) {
293
- throw new \InvalidArgumentException ('Failed to save alpha transparency into PNG image. ' );
294
- }
295
- } elseif (false !== $ transparentIndex ) {
296
- // fill image with indexed non-alpha transparency
297
- $ transparentColor = false ;
298
- if ($ transparentIndex >= 0 && $ transparentIndex <= imagecolorstotal ($ this ->_imageHandler )) {
299
- list ($ r , $ g , $ b ) = array_values (imagecolorsforindex ($ this ->_imageHandler , $ transparentIndex ));
300
- $ transparentColor = imagecolorallocate ($ imageResourceTo , $ r , $ g , $ b );
301
- }
302
- if (false === $ transparentColor ) {
303
- throw new \InvalidArgumentException ('Failed to allocate transparent color for image. ' );
304
- }
305
- if (!imagefill ($ imageResourceTo , 0 , 0 , $ transparentColor )) {
306
- throw new \InvalidArgumentException ('Failed to fill image with transparency. ' );
307
- }
308
- imagecolortransparent ($ imageResourceTo , $ transparentColor );
282
+ $ this ->applyAlphaTransparency ($ imageResourceTo );
283
+
284
+ return ;
285
+ }
286
+
287
+ if ($ transparentIndex !== false ) {
288
+ $ this ->applyTransparency ($ imageResourceTo , $ transparentIndex );
289
+
290
+ return ;
309
291
}
310
292
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
311
293
} catch (\Exception $ e ) {
312
294
// fallback to default background color
313
295
}
314
296
}
315
297
list ($ red , $ green , $ blue ) = $ this ->_backgroundColor ;
298
+ $ red = $ red !== null ? $ red : 0 ;
299
+ $ green = $ green !== null ? $ green : 0 ;
300
+ $ blue = $ blue !== null ? $ blue : 0 ;
301
+ $ color = imagecolorallocate ($ imageResourceTo , $ red , $ green , $ blue );
316
302
317
- if ($ imageResourceTo && $ red && $ green && $ blue ) {
318
- $ color = imagecolorallocate ($ imageResourceTo , $ red , $ green , $ blue );
303
+ if (!imagefill ($ imageResourceTo , 0 , 0 , $ color )) {
304
+ throw new \InvalidArgumentException ("Failed to fill image background with color {$ red } {$ green } {$ blue }. " );
305
+ }
306
+ }
319
307
320
- if (!$ color && !imagefill ($ imageResourceTo , 0 , 0 , $ color )) {
321
- throw new \InvalidArgumentException (
322
- "Failed to fill image background with color {$ red } {$ green } {$ blue }. "
323
- );
324
- }
308
+ /**
309
+ * Method to apply alpha transparency for image.
310
+ *
311
+ * @param resource $imageResourceTo
312
+ *
313
+ * @return void
314
+ * @SuppressWarnings(PHPMD.LongVariable)
315
+ */
316
+ private function applyAlphaTransparency (&$ imageResourceTo ): void
317
+ {
318
+ if (!imagealphablending ($ imageResourceTo , false )) {
319
+ throw new \InvalidArgumentException ('Failed to set alpha blending for PNG image. ' );
320
+ }
321
+ $ transparentAlphaColor = imagecolorallocatealpha ($ imageResourceTo , 0 , 0 , 0 , 127 );
322
+
323
+ if (false === $ transparentAlphaColor ) {
324
+ throw new \InvalidArgumentException ('Failed to allocate alpha transparency for PNG image. ' );
325
+ }
326
+
327
+ if (!imagefill ($ imageResourceTo , 0 , 0 , $ transparentAlphaColor )) {
328
+ throw new \InvalidArgumentException ('Failed to fill PNG image with alpha transparency. ' );
329
+ }
330
+
331
+ if (!imagesavealpha ($ imageResourceTo , true )) {
332
+ throw new \InvalidArgumentException ('Failed to save alpha transparency into PNG image. ' );
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Method to apply transparency for image.
338
+ *
339
+ * @param resource $imageResourceTo
340
+ * @param int $transparentIndex
341
+ *
342
+ * @return void
343
+ */
344
+ private function applyTransparency (&$ imageResourceTo , $ transparentIndex ): void
345
+ {
346
+ // fill image with indexed non-alpha transparency
347
+ $ transparentColor = false ;
348
+
349
+ if ($ transparentIndex >= 0 && $ transparentIndex <= imagecolorstotal ($ this ->_imageHandler )) {
350
+ list ($ red , $ green , $ blue ) = array_values (imagecolorsforindex ($ this ->_imageHandler , $ transparentIndex ));
351
+ $ transparentColor = imagecolorallocate ($ imageResourceTo , (int ) $ red , (int ) $ green , (int ) $ blue );
352
+ }
353
+ if (false === $ transparentColor ) {
354
+ throw new \InvalidArgumentException ('Failed to allocate transparent color for image. ' );
355
+ }
356
+ if (!imagefill ($ imageResourceTo , 0 , 0 , $ transparentColor )) {
357
+ throw new \InvalidArgumentException ('Failed to fill image with transparency. ' );
325
358
}
359
+ imagecolortransparent ($ imageResourceTo , $ transparentColor );
326
360
}
327
361
328
362
/**
@@ -398,7 +432,7 @@ public function resize($frameWidth = null, $frameHeight = null)
398
432
}
399
433
400
434
// fill new image with required color
401
- $ this ->_fillBackgroundColor ($ newImage );
435
+ $ this ->fillBackgroundColor ($ newImage );
402
436
403
437
if ($ this ->_imageHandler ) {
404
438
// resample source image and copy it into new frame
0 commit comments