@@ -61,9 +61,9 @@ protected function _reset()
6161 */
6262 public function open ($ filename )
6363 {
64- if (! $ filename || !file_exists ($ filename )) {
64+ if ($ filename === null || !file_exists ($ filename )) {
6565 throw new FileSystemException (
66- new Phrase ('File "%1" does not exist. ' , [$ this -> _fileName ])
66+ new Phrase ('File "%1" does not exist. ' , [$ filename ])
6767 );
6868 }
6969 if (!$ filename || filesize ($ filename ) === 0 || !$ this ->validateURLScheme ($ filename )) {
@@ -188,7 +188,7 @@ public function save($destination = null, $newName = null)
188188 } else {
189189 $ newImage = imagecreate ($ this ->_imageSrcWidth , $ this ->_imageSrcHeight );
190190 }
191- $ this ->_fillBackgroundColor ($ newImage );
191+ $ this ->fillBackgroundColor ($ newImage );
192192 imagecopy ($ newImage , $ this ->_imageHandler , 0 , 0 , 0 , 0 , $ this ->_imageSrcWidth , $ this ->_imageSrcHeight );
193193 $ this ->imageDestroy ();
194194 $ this ->_imageHandler = $ newImage ;
@@ -265,64 +265,98 @@ private function _getCallback($callbackType, $fileType = null, $unsupportedText
265265 * Returns a color identifier.
266266 *
267267 * @param resource &$imageResourceTo
268+ *
268269 * @return void
269270 * @throws \InvalidArgumentException
270- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
271- * @SuppressWarnings(PHPMD.NPathComplexity)
272271 */
273- private function _fillBackgroundColor (&$ imageResourceTo )
272+ private function fillBackgroundColor (&$ imageResourceTo ): void
274273 {
275274 // try to keep transparency, if any
276275 if ($ this ->_keepTransparency ) {
277276 $ isAlpha = false ;
278277 $ transparentIndex = $ this ->_getTransparency ($ this ->_imageHandler , $ this ->_fileType , $ isAlpha );
278+
279279 try {
280- // fill truecolor png with alpha transparency
280+ // fill true color png with alpha transparency
281281 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 ;
309291 }
310292 // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
311293 } catch (\Exception $ e ) {
312294 // fallback to default background color
313295 }
314296 }
315297 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 );
316302
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+ }
319307
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. ' );
325358 }
359+ imagecolortransparent ($ imageResourceTo , $ transparentColor );
326360 }
327361
328362 /**
@@ -398,7 +432,7 @@ public function resize($frameWidth = null, $frameHeight = null)
398432 }
399433
400434 // fill new image with required color
401- $ this ->_fillBackgroundColor ($ newImage );
435+ $ this ->fillBackgroundColor ($ newImage );
402436
403437 if ($ this ->_imageHandler ) {
404438 // resample source image and copy it into new frame
0 commit comments