From 9c488c1968a9b4968dee57c7bcd3028c51f9c227 Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Tue, 25 Jul 2023 09:48:14 +0200 Subject: [PATCH 1/4] add check to image create functions --- src/Image/Operation/ToJpg.php | 5 +++++ src/Image/Operation/ToWebp.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/Image/Operation/ToJpg.php b/src/Image/Operation/ToJpg.php index 979ec6ba7..c7c9adad5 100644 --- a/src/Image/Operation/ToJpg.php +++ b/src/Image/Operation/ToJpg.php @@ -67,6 +67,11 @@ public function run($load_filename, $save_filename) $input = $imagecreate_function($load_filename); + if (!$input) { + Helper::error_log('The function ' . $imagecreate_function . ' failed to create a valid image from ' . $load_filename . '.'); + return false; + } + list($width, $height) = getimagesize($load_filename); $output = imagecreatetruecolor($width, $height); $c = self::hexrgb($this->color); diff --git a/src/Image/Operation/ToWebp.php b/src/Image/Operation/ToWebp.php index 5c576be02..56e84f6cc 100644 --- a/src/Image/Operation/ToWebp.php +++ b/src/Image/Operation/ToWebp.php @@ -68,6 +68,11 @@ public function run($load_filename, $save_filename) $input = $imagecreate_function($load_filename); + if (!$input) { + Helper::error_log('The function ' . $imagecreate_function . ' failed to create a valid image from ' . $load_filename . '.'); + return false; + } + if (!imageistruecolor($input)) { imagepalettetotruecolor($input); } From 5ddd10a0c7abd8cb7a2bb33fddb483a013d56790 Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Tue, 25 Jul 2023 14:18:28 +0200 Subject: [PATCH 2/4] remove error_log and prefix functions --- src/Image/Operation/ToJpg.php | 5 ++--- src/Image/Operation/ToWebp.php | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Image/Operation/ToJpg.php b/src/Image/Operation/ToJpg.php index c7c9adad5..e30150e8e 100644 --- a/src/Image/Operation/ToJpg.php +++ b/src/Image/Operation/ToJpg.php @@ -68,12 +68,11 @@ public function run($load_filename, $save_filename) $input = $imagecreate_function($load_filename); if (!$input) { - Helper::error_log('The function ' . $imagecreate_function . ' failed to create a valid image from ' . $load_filename . '.'); return false; } - list($width, $height) = getimagesize($load_filename); - $output = imagecreatetruecolor($width, $height); + list($width, $height) = \getimagesize($load_filename); + $output = \imagecreatetruecolor($width, $height); $c = self::hexrgb($this->color); $color = imagecolorallocate($output, $c['red'], $c['green'], $c['blue']); imagefilledrectangle($output, 0, 0, $width, $height, $color); diff --git a/src/Image/Operation/ToWebp.php b/src/Image/Operation/ToWebp.php index 56e84f6cc..bc910fca0 100644 --- a/src/Image/Operation/ToWebp.php +++ b/src/Image/Operation/ToWebp.php @@ -69,12 +69,11 @@ public function run($load_filename, $save_filename) $input = $imagecreate_function($load_filename); if (!$input) { - Helper::error_log('The function ' . $imagecreate_function . ' failed to create a valid image from ' . $load_filename . '.'); return false; } - if (!imageistruecolor($input)) { - imagepalettetotruecolor($input); + if (!\imageistruecolor($input)) { + \imagepalettetotruecolor($input); } if (!function_exists('imagewebp')) { From 5a62c957bbb7ad05b578cf54309a0fdd2fc028ab Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Fri, 28 Jul 2023 14:54:26 +0200 Subject: [PATCH 3/4] Check expected values as suggested by @nlemoine --- src/Image/Operation/ToJpg.php | 3 ++- src/Image/Operation/ToWebp.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Image/Operation/ToJpg.php b/src/Image/Operation/ToJpg.php index ae017a93b..c4d327176 100644 --- a/src/Image/Operation/ToJpg.php +++ b/src/Image/Operation/ToJpg.php @@ -2,6 +2,7 @@ namespace Timber\Image\Operation; +use GdImage; use Timber\Image\Operation as ImageOperation; use Timber\ImageHelper; @@ -67,7 +68,7 @@ public function run($load_filename, $save_filename) $input = $imagecreate_function($load_filename); - if (!$input) { + if (!$input instanceof GdImage) { return false; } diff --git a/src/Image/Operation/ToWebp.php b/src/Image/Operation/ToWebp.php index 6004c1c90..ba34a66fe 100644 --- a/src/Image/Operation/ToWebp.php +++ b/src/Image/Operation/ToWebp.php @@ -2,6 +2,7 @@ namespace Timber\Image\Operation; +use GdImage; use Timber\Helper; use Timber\Image\Operation as ImageOperation; use Timber\ImageHelper; @@ -68,7 +69,7 @@ public function run($load_filename, $save_filename) $input = $imagecreate_function($load_filename); - if (!$input) { + if (!$input instanceof GdImage) { return false; } From cb9243f6b4e45f9c5158e96affe9afa1435d8ee0 Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Fri, 28 Jul 2023 16:14:29 +0200 Subject: [PATCH 4/4] Update statements to Weak implicit Co-authored-by: Nicolas Lemoine --- src/Image/Operation/ToJpg.php | 3 +-- src/Image/Operation/ToWebp.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Image/Operation/ToJpg.php b/src/Image/Operation/ToJpg.php index c4d327176..6f768e8b0 100644 --- a/src/Image/Operation/ToJpg.php +++ b/src/Image/Operation/ToJpg.php @@ -2,7 +2,6 @@ namespace Timber\Image\Operation; -use GdImage; use Timber\Image\Operation as ImageOperation; use Timber\ImageHelper; @@ -68,7 +67,7 @@ public function run($load_filename, $save_filename) $input = $imagecreate_function($load_filename); - if (!$input instanceof GdImage) { + if ($input === false) { return false; } diff --git a/src/Image/Operation/ToWebp.php b/src/Image/Operation/ToWebp.php index ba34a66fe..11b4ba5a9 100644 --- a/src/Image/Operation/ToWebp.php +++ b/src/Image/Operation/ToWebp.php @@ -2,7 +2,6 @@ namespace Timber\Image\Operation; -use GdImage; use Timber\Helper; use Timber\Image\Operation as ImageOperation; use Timber\ImageHelper; @@ -69,7 +68,7 @@ public function run($load_filename, $save_filename) $input = $imagecreate_function($load_filename); - if (!$input instanceof GdImage) { + if ($input === false) { return false; }