Skip to content

Commit

Permalink
Add webp to images support
Browse files Browse the repository at this point in the history
See SimpleMachines#6271

Signed-off-by: Jon Stovell <[email protected]>
  • Loading branch information
Sesquipedalian committed Jun 26, 2024
1 parent 8b2be60 commit 33f1686
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/Languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/Membergroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = [];
Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageManager/PackageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = '<img src="' . Config::$scripturl . '?action=admin;area=packages;sa=examine;package=' . $_REQUEST['package'] . ';file=' . $_REQUEST['file'] . ';raw" alt="' . $_REQUEST['file'] . '">';
} else {
if (is_file(Config::$packagesdir . '/' . $_REQUEST['package'])) {
Expand Down
3 changes: 2 additions & 1 deletion Sources/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Tasks/ExportProfileData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion Themes/default/Profile.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2940,7 +2940,7 @@ function template_profile_avatar_select()
echo '
<div id="avatar_upload">
', Utils::$context['member']['avatar']['choice'] == 'upload' ? '<div class="edit_avatar_img"><img src="' . Utils::$context['member']['avatar']['href'] . '" alt=""></div>' : '', '
<input type="file" size="44" name="attachment" id="avatar_upload_box" value="" onchange="readfromUpload(this)" onfocus="selectRadioByName(document.forms.creator.avatar_choice, \'upload\');" accept="image/gif, image/jpeg, image/jpg, image/png">', template_max_size('upload'), '
<input type="file" size="44" name="attachment" id="avatar_upload_box" value="" onchange="readfromUpload(this)" onfocus="selectRadioByName(document.forms.creator.avatar_choice, \'upload\');" accept="image/gif, image/jpeg, image/jpg, image/png, image/svg+xml, image/webp">', template_max_size('upload'), '
', (!empty(Utils::$context['member']['avatar']['id_attach']) ? '<br><input type="hidden" name="id_attach" value="' . Utils::$context['member']['avatar']['id_attach'] . '">' : ''), '
</div>';

Expand Down
2 changes: 1 addition & 1 deletion Themes/default/scripts/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 33f1686

Please sign in to comment.