From 33f1686f992315e2a2cc0818cb87c5147c057ac5 Mon Sep 17 00:00:00 2001 From: Jon Stovell Date: Wed, 26 Jun 2024 15:47:18 -0600 Subject: [PATCH] Add webp to images support See #6271 Signed-off-by: Jon Stovell --- Sources/Actions/Admin/Languages.php | 2 +- Sources/Actions/Admin/Membergroups.php | 2 +- Sources/Actions/Admin/Themes.php | 2 +- Sources/PackageManager/PackageManager.php | 2 +- Sources/Profile.php | 3 ++- Sources/Tasks/ExportProfileData.php | 1 + Sources/Utils.php | 2 +- Themes/default/Profile.template.php | 2 +- Themes/default/scripts/admin.js | 2 +- 9 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Sources/Actions/Admin/Languages.php b/Sources/Actions/Admin/Languages.php index bdec93ce8f..1a574f4d6c 100644 --- a/Sources/Actions/Admin/Languages.php +++ b/Sources/Actions/Admin/Languages.php @@ -246,7 +246,7 @@ public function download(): void $extension = $pathinfo['extension']; // Don't do anything with files we don't understand. - if (!in_array($extension, ['php', 'jpg', 'gif', 'jpeg', 'png', 'txt'])) { + if (!in_array($extension, ['php', 'jpg', 'gif', 'jpeg', 'png', 'txt', 'webp'])) { continue; } diff --git a/Sources/Actions/Admin/Membergroups.php b/Sources/Actions/Admin/Membergroups.php index 5c0e731a3f..cfcebbc37b 100644 --- a/Sources/Actions/Admin/Membergroups.php +++ b/Sources/Actions/Admin/Membergroups.php @@ -655,7 +655,7 @@ public function edit(): void Utils::$context['is_moderator_group'] = $group->is_moderator_group; // Get a list of all the image formats we can select for icons. - $imageExts = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'svg']; + $imageExts = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'svg', 'webp']; // Scan the icons directory. Utils::$context['possible_icons'] = []; diff --git a/Sources/Actions/Admin/Themes.php b/Sources/Actions/Admin/Themes.php index 0dfeee1f01..28cd05691b 100644 --- a/Sources/Actions/Admin/Themes.php +++ b/Sources/Actions/Admin/Themes.php @@ -2219,7 +2219,7 @@ protected function getFileList(string $path, string $relative): array 'is_writable' => is_writable($path . '/' . $entry), 'is_directory' => false, 'is_template' => preg_match('~\.template\.php$~', $entry) != 0, - 'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png)$~', $entry) != 0, + 'is_image' => preg_match('~\.(jpg|jpeg|gif|bmp|png|svg|webp)$~', $entry) != 0, 'is_editable' => is_writable($path . '/' . $entry) && preg_match('~\.(php|pl|css|js|vbs|xml|xslt|txt|xsl|html|htm|shtm|shtml|asp|aspx|cgi|py)$~', $entry) != 0, 'href' => Config::$scripturl . '?action=admin;area=theme;th=' . $_GET['th'] . ';' . Utils::$context['session_var'] . '=' . Utils::$context['session_id'] . ';sa=edit;filename=' . $relative . $entry, 'size' => $size, diff --git a/Sources/PackageManager/PackageManager.php b/Sources/PackageManager/PackageManager.php index c6f1aa935e..4db0c4960d 100644 --- a/Sources/PackageManager/PackageManager.php +++ b/Sources/PackageManager/PackageManager.php @@ -1451,7 +1451,7 @@ public function examineFile(): void Utils::$context['filename'] = $_REQUEST['file']; // Let the unpacker do the work.... but make sure we handle images properly. - if (in_array(strtolower(strrchr($_REQUEST['file'], '.')), ['.bmp', '.gif', '.jpeg', '.jpg', '.png'])) { + if (in_array(strtolower(strrchr($_REQUEST['file'], '.')), ['.bmp', '.gif', '.jpeg', '.jpg', '.png', '.webp'])) { Utils::$context['filedata'] = '' . $_REQUEST['file'] . ''; } else { if (is_file(Config::$packagesdir . '/' . $_REQUEST['package'])) { diff --git a/Sources/Profile.php b/Sources/Profile.php index 1ec4dc4bc7..ee11fb2d07 100644 --- a/Sources/Profile.php +++ b/Sources/Profile.php @@ -779,7 +779,7 @@ public function loadStandardFields(bool $force_reload = false) $no_smiley_sets = array_diff(explode(',', Config::$modSettings['smiley_sets_known']), array_keys($filenames)); foreach ($no_smiley_sets as $set) { - $allowedTypes = ['gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg']; + $allowedTypes = ['gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg', 'webp']; $images = glob(implode('/', [Config::$modSettings['smileys_dir'], $set, '*.{' . (implode(',', $allowedTypes) . '}')]), GLOB_BRACE); // Just use some image or other @@ -2607,6 +2607,7 @@ protected function getAvatars(string $directory, int $level = 0): array && strcasecmp($extension, 'jpeg') != 0 && strcasecmp($extension, 'png') != 0 && strcasecmp($extension, 'bmp') != 0 + && strcasecmp($extension, 'webp') != 0 ) { continue; } diff --git a/Sources/Tasks/ExportProfileData.php b/Sources/Tasks/ExportProfileData.php index e59d4839b2..7c6a7a1b02 100644 --- a/Sources/Tasks/ExportProfileData.php +++ b/Sources/Tasks/ExportProfileData.php @@ -1797,6 +1797,7 @@ function ($a, $b) { 'jpeg' => 'image/jpeg', 'tiff' => 'image/tiff', 'svg' => 'image/svg+xml', + 'webp' => 'image/webp', ]; foreach (glob(implode(DIRECTORY_SEPARATOR, [Config::$modSettings['smileys_dir'], User::$me->smiley_set, '*.*'])) as $smiley_file) { diff --git a/Sources/Utils.php b/Sources/Utils.php index dd03af4064..90f4ebbc9c 100644 --- a/Sources/Utils.php +++ b/Sources/Utils.php @@ -1831,7 +1831,7 @@ public static function emitFile(\ArrayAccess|array $file, bool $show_thumb = fal } // If this has an "image extension" - but isn't actually an image - then ensure it isn't cached cause of silly IE. - if (isset($file['mime_type'], $file['fileext']) && !str_starts_with($file['mime_type'], 'image/') && in_array($file['fileext'], ['gif', 'jpg', 'bmp', 'png', 'jpeg', 'tiff'])) { + if (isset($file['mime_type'], $file['fileext']) && !str_starts_with($file['mime_type'], 'image/') && in_array($file['fileext'], ['gif', 'jpg', 'bmp', 'png', 'jpeg', 'tiff', 'webp'])) { header('Cache-Control: no-cache'); } else { header('Cache-Control: max-age=' . (525600 * 60) . ', private'); diff --git a/Themes/default/Profile.template.php b/Themes/default/Profile.template.php index 20d06b4d11..a3d13ff4cc 100644 --- a/Themes/default/Profile.template.php +++ b/Themes/default/Profile.template.php @@ -2940,7 +2940,7 @@ function template_profile_avatar_select() echo '
', Utils::$context['member']['avatar']['choice'] == 'upload' ? '
' : '', ' - ', template_max_size('upload'), ' + ', template_max_size('upload'), ' ', (!empty(Utils::$context['member']['avatar']['id_attach']) ? '
' : ''), '
'; diff --git a/Themes/default/scripts/admin.js b/Themes/default/scripts/admin.js index f405327884..efcc6437b2 100644 --- a/Themes/default/scripts/admin.js +++ b/Themes/default/scripts/admin.js @@ -507,7 +507,7 @@ function updatePreview(filename, filepath) relative_url = "/" + filepath + "/" + filename; // Make sure no sneaky people are trying to be sneaky - var regex = new RegExp("^/(" + smf_smiley_sets.split(",").join("|") + ")/[^.]+\.(gif|png|jpg|jpeg|tiff|svg)$"); + var regex = new RegExp("^/(" + smf_smiley_sets.split(",").join("|") + ")/[^.]+\.(gif|png|jpg|jpeg|tiff|svg|webp)$"); var is_valid = relative_url.match(regex); if (is_valid !== null)