From 1e2b958786dbc68aae5ff20468646be979605f49 Mon Sep 17 00:00:00 2001 From: Luca Battarra Date: Tue, 21 Feb 2023 09:14:00 +0100 Subject: [PATCH 1/2] Fix: Uploading a GIF results in ArgumentCountError --- src/Repository/BaseUploadHandler.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Repository/BaseUploadHandler.php b/src/Repository/BaseUploadHandler.php index c535813..83f945f 100644 --- a/src/Repository/BaseUploadHandler.php +++ b/src/Repository/BaseUploadHandler.php @@ -683,6 +683,21 @@ protected function gd_orient_image($file, $src_img) { return true; } + protected function gd_write_func($type, $src_img, $new_file_path, $image_quality = null) + { + switch ($type) { + case 'jpg': + case 'jpeg': + return imagejpeg($src_img, $new_file_path, $image_quality); + case 'gif': + return imagegif($src_img, $new_file_path); + case 'png': + return imagepng($src_img, $new_file_path, $image_quality); + } + + return false; + } + protected function gd_create_scaled_image($file, $version, $options) { if (!function_exists('imagecreatetruecolor')) { error_log('Function not found: imagecreatetruecolor'); @@ -694,18 +709,15 @@ protected function gd_create_scaled_image($file, $version, $options) { case 'jpg': case 'jpeg': $src_func = 'imagecreatefromjpeg'; - $write_func = 'imagejpeg'; $image_quality = isset($options['jpeg_quality']) ? $options['jpeg_quality'] : 75; break; case 'gif': $src_func = 'imagecreatefromgif'; - $write_func = 'imagegif'; $image_quality = null; break; case 'png': $src_func = 'imagecreatefrompng'; - $write_func = 'imagepng'; $image_quality = isset($options['png_quality']) ? $options['png_quality'] : 9; break; @@ -742,7 +754,7 @@ protected function gd_create_scaled_image($file, $version, $options) { ); if ($scale >= 1) { if ($image_oriented) { - return $write_func($src_img, $new_file_path, $image_quality); + return $this->gd_write_func($type, $src_img, $new_file_path, $image_quality); } if ($file->path !== $new_file_path) { return copy($file->path, $new_file_path); @@ -788,7 +800,7 @@ protected function gd_create_scaled_image($file, $version, $options) { $new_height, $img_width, $img_height - ) && $write_func($new_img, $new_file_path, $image_quality); + ) && $this->gd_write_func($type, $src_img, $new_file_path, $image_quality); $this->gd_set_image_object($file->path, $new_img); return $success; } From 9832b48e06e46f6ade362d7f9706d9036816e25b Mon Sep 17 00:00:00 2001 From: Luca Battarra Date: Mon, 15 May 2023 15:12:33 +0200 Subject: [PATCH 2/2] Handle when file content does not match file extension --- src/Repository/BaseUploadHandler.php | 3 ++- src/Repository/Local/UploadHandler.php | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Repository/BaseUploadHandler.php b/src/Repository/BaseUploadHandler.php index 83f945f..5700bb4 100644 --- a/src/Repository/BaseUploadHandler.php +++ b/src/Repository/BaseUploadHandler.php @@ -46,7 +46,8 @@ class BaseUploadHandler 'max_height' => 'Image exceeds maximum height', 'min_height' => 'Image requires a minimum height', 'abort' => 'File upload aborted', - 'image_resize' => 'Failed to resize image' + 'image_resize' => 'Failed to resize image', + 'type_mismatch' => 'File content does not match file extension', ); protected $image_objects = array(); diff --git a/src/Repository/Local/UploadHandler.php b/src/Repository/Local/UploadHandler.php index 7a9bc0f..dca6af3 100644 --- a/src/Repository/Local/UploadHandler.php +++ b/src/Repository/Local/UploadHandler.php @@ -195,6 +195,11 @@ function_exists('exif_read_data') && return false; } } + $img_info = $this->get_image_size($uploaded_file); + if ($img_info['mime'] !== $file->type) { + $file->error = $this->get_error_message('type_mismatch'); + return false; + } return true; }