@@ -415,6 +415,26 @@ public function checkAlpha($fileName)
415415 return (ord (file_get_contents ((string )$ fileName , false , null , 25 , 1 )) & 6 & 4 ) == 4 ;
416416 }
417417
418+ /**
419+ * Gives true for a WebP with alpha, false otherwise
420+ *
421+ * @param string $filename
422+ * @return bool
423+ */
424+ public function checkAlphaWebp ($ filename )
425+ {
426+ $ buf = file_get_contents ((string ) $ filename , false , null , 0 , 25 );
427+ if ($ buf [15 ] === 'L ' ) {
428+ // Simple File Format (Lossless)
429+ return (bool ) (!!(ord ($ buf [24 ]) & 0x00000010 ));
430+ } elseif ($ buf [15 ] === 'X ' ) {
431+ // Extended File Format
432+ return (bool ) (!!(ord ($ buf [20 ]) & 0x00000010 ));
433+ }
434+
435+ return false ;
436+ }
437+
418438 /**
419439 * Checks if image has alpha transparency
420440 *
@@ -445,12 +465,18 @@ private function _getTransparency($imageResource, $fileType, &$isAlpha = false,
445465 return $ transparentIndex ;
446466 }
447467 }
468+ if (IMAGETYPE_JPEG === $ fileType ) {
469+ $ isTrueColor = true ;
470+ }
471+
448472 /*
449- * FIX: added '|| IMAGETYPE_WEBP === $fileType' for the condition
450- */
451- if (IMAGETYPE_JPEG === $ fileType || IMAGETYPE_WEBP === $ fileType ) {
473+ * FIX: prepare transparency data for WebP image
474+ */
475+ if (IMAGETYPE_WEBP === $ fileType ) {
452476 $ isTrueColor = true ;
477+ $ isAlpha = $ this ->checkAlphaWebp ($ this ->_fileName );
453478 }
479+
454480 return false ;
455481 }
456482
0 commit comments