From f09241fafe70ed137a556b2ab0c449b94ca48841 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 5 Jul 2023 00:54:03 +0100 Subject: [PATCH] MDL-78644 files: more fault tolerant resizing of logos/icons. If we cannot resize the given file (files such as ICO can't be, for SVG it makes no sense), then we should just return the original file as is rather than an error. --- admin/lib.php | 12 ++++-------- lib/filestorage/stored_file.php | 3 +++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/admin/lib.php b/admin/lib.php index d9a722d818a..8a9f0eeef6b 100644 --- a/admin/lib.php +++ b/admin/lib.php @@ -96,20 +96,16 @@ function core_admin_pluginfile($course, $cm, $context, $filearea, $args, $forced send_file_not_found(); } - // No need for resizing, but if the file should be cached we save it so we can serve it fast next time. - if (empty($maxwidth) && empty($maxheight)) { + // Check whether width/height are specified, and we can resize the image (some types such as ICO cannot be resized). + if (($maxwidth === 0 && $maxheight === 0) || + !$filedata = $file->resize_image($maxwidth, $maxheight)) { + if ($lifetime) { file_safe_save_content($file->get_content(), $candidate); } send_stored_file($file, $lifetime, 0, false, $options); } - // Proceed with the resizing. - $filedata = $file->resize_image($maxwidth, $maxheight); - if (!$filedata) { - send_file_not_found(); - } - // If we don't want to cached the file, serve now and quit. if (!$lifetime) { send_content_uncached($filedata, $filename); diff --git a/lib/filestorage/stored_file.php b/lib/filestorage/stored_file.php index d04b236b9b0..607e7ce2657 100644 --- a/lib/filestorage/stored_file.php +++ b/lib/filestorage/stored_file.php @@ -1127,6 +1127,9 @@ class stored_file { // Create a new image from the file. $original = @imagecreatefromstring($content); + if (empty($original)) { + return false; + } // Generate the resized image. return resize_image_from_image($original, $imageinfo, $width, $height);