From 0ebfada98b89390b5e609dd04448f26075c15453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Viguier?= Date: Mon, 11 Mar 2024 12:37:37 +0100 Subject: [PATCH 1/7] If png color type is 3, the file is paletted (256 colors). Change palette to reduce file size --- classes/ImageManager.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/classes/ImageManager.php b/classes/ImageManager.php index f0359ede7e8ce..6cd78386da87e 100644 --- a/classes/ImageManager.php +++ b/classes/ImageManager.php @@ -309,6 +309,10 @@ public static function resize( imagealphablending($destImage, false); imagesavealpha($destImage, true); $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127); + // if png color type is 3, the file is paletted (256 colors). Change palette to reduce file size + if (self::getPNGColorType($sourceFile) == 3) { + imagetruecolortopalette($destImage, false, 255); + } imagefilledrectangle($destImage, 0, 0, $destinationWidth, $destinationHeight, $transparent); } else { $white = imagecolorallocate($destImage, 255, 255, 255); @@ -899,4 +903,26 @@ public static function get_best_path($tgt_width, $tgt_height, $path_infos) return $path; } + + /** + * The function `getPNGColorType` returns the color type byte from a PNG file + * + * @param string $fileName + * + * @return @return int|bool + */ + public static function getPNGColorType($fileName) + { + $handle = fopen($fileName, 'r'); + if (false === $handle) { + return false; + } + + // set pointer to the color type byte and read it + fseek($handle, 25); + $colorTypeByte = fread($handle, 1); + fclose($handle); + + return ord($colorTypeByte); + } } From f87d7e26c0a2e75af1025ae4b3a466f7f42b760d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Viguier?= Date: Mon, 11 Mar 2024 16:22:45 +0100 Subject: [PATCH 2/7] typo --- classes/ImageManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/ImageManager.php b/classes/ImageManager.php index 6cd78386da87e..15a962f95165d 100644 --- a/classes/ImageManager.php +++ b/classes/ImageManager.php @@ -909,7 +909,7 @@ public static function get_best_path($tgt_width, $tgt_height, $path_infos) * * @param string $fileName * - * @return @return int|bool + * @return int|bool */ public static function getPNGColorType($fileName) { From 6575d7c06ae91f99ca118af9f17cc2e8e848773e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Viguier?= Date: Mon, 11 Mar 2024 17:36:20 +0100 Subject: [PATCH 3/7] bug fix with 8bits png with a transparent color --- classes/ImageManager.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/classes/ImageManager.php b/classes/ImageManager.php index 15a962f95165d..4f81ca9605cd1 100644 --- a/classes/ImageManager.php +++ b/classes/ImageManager.php @@ -306,13 +306,14 @@ public static function resize( // If the output is PNG, fill with transparency. Else fill with white background. if ($destinationFileType == 'png' || $destinationFileType == 'webp' || $destinationFileType == 'avif') { - imagealphablending($destImage, false); - imagesavealpha($destImage, true); - $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127); // if png color type is 3, the file is paletted (256 colors). Change palette to reduce file size if (self::getPNGColorType($sourceFile) == 3) { imagetruecolortopalette($destImage, false, 255); + } else { + imagealphablending($destImage, false); } + imagesavealpha($destImage, true); + $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127); imagefilledrectangle($destImage, 0, 0, $destinationWidth, $destinationHeight, $transparent); } else { $white = imagecolorallocate($destImage, 255, 255, 255); From 36377a137cb7a424b727ae35eaab9d1cf0fc7686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Viguier?= Date: Mon, 11 Mar 2024 18:03:58 +0100 Subject: [PATCH 4/7] file read improvement --- classes/ImageManager.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/classes/ImageManager.php b/classes/ImageManager.php index 4f81ca9605cd1..30bf6328613ac 100644 --- a/classes/ImageManager.php +++ b/classes/ImageManager.php @@ -914,16 +914,10 @@ public static function get_best_path($tgt_width, $tgt_height, $path_infos) */ public static function getPNGColorType($fileName) { - $handle = fopen($fileName, 'r'); - if (false === $handle) { + if (!is_readable($fileName)) { return false; } - // set pointer to the color type byte and read it - fseek($handle, 25); - $colorTypeByte = fread($handle, 1); - fclose($handle); - - return ord($colorTypeByte); + return ord(@file_get_contents($fileName, false, null, 25, 1)); } } From cc4a0408104c8fa25be497a8e6c735c48fa27326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Viguier?= Date: Tue, 12 Mar 2024 09:51:29 +0100 Subject: [PATCH 5/7] Palette stuff only for png --- classes/ImageManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/ImageManager.php b/classes/ImageManager.php index 30bf6328613ac..a711613529057 100644 --- a/classes/ImageManager.php +++ b/classes/ImageManager.php @@ -307,7 +307,7 @@ public static function resize( // If the output is PNG, fill with transparency. Else fill with white background. if ($destinationFileType == 'png' || $destinationFileType == 'webp' || $destinationFileType == 'avif') { // if png color type is 3, the file is paletted (256 colors). Change palette to reduce file size - if (self::getPNGColorType($sourceFile) == 3) { + if ($destinationFileType == 'png' && self::getPNGColorType($sourceFile) == 3) { imagetruecolortopalette($destImage, false, 255); } else { imagealphablending($destImage, false); From 2f3092de65fb66c7454d4d3b3564160c03662de6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Viguier?= Date: Tue, 12 Mar 2024 13:30:16 +0100 Subject: [PATCH 6/7] litle refacto for SharakPL --- classes/ImageManager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/ImageManager.php b/classes/ImageManager.php index a711613529057..aa5d0aed35d20 100644 --- a/classes/ImageManager.php +++ b/classes/ImageManager.php @@ -305,8 +305,8 @@ public static function resize( $destImage = imagecreatetruecolor($destinationWidth, $destinationHeight); // If the output is PNG, fill with transparency. Else fill with white background. - if ($destinationFileType == 'png' || $destinationFileType == 'webp' || $destinationFileType == 'avif') { - // if png color type is 3, the file is paletted (256 colors). Change palette to reduce file size + if (in_array($destinationFileType, ['png', 'webp', 'avif'])) { + // if png color type is 3, the file is paletted (256 colors or less). Change palette to reduce file size if ($destinationFileType == 'png' && self::getPNGColorType($sourceFile) == 3) { imagetruecolortopalette($destImage, false, 255); } else { From 6793d47957375d24c46f5bcc070d7b1dc0ad0b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Viguier?= Date: Tue, 2 Jul 2024 15:39:52 +0200 Subject: [PATCH 7/7] check source file type for png --- classes/ImageManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/ImageManager.php b/classes/ImageManager.php index aa5d0aed35d20..8bcb94d6dc5a3 100644 --- a/classes/ImageManager.php +++ b/classes/ImageManager.php @@ -307,7 +307,7 @@ public static function resize( // If the output is PNG, fill with transparency. Else fill with white background. if (in_array($destinationFileType, ['png', 'webp', 'avif'])) { // if png color type is 3, the file is paletted (256 colors or less). Change palette to reduce file size - if ($destinationFileType == 'png' && self::getPNGColorType($sourceFile) == 3) { + if ($destinationFileType == 'png' && $sourceFileType == IMAGETYPE_PNG && self::getPNGColorType($sourceFile) == 3) { imagetruecolortopalette($destImage, false, 255); } else { imagealphablending($destImage, false);