diff --git a/public/index.html b/public/index.html index 280a30c1b..24cd3db85 100644 --- a/public/index.html +++ b/public/index.html @@ -4008,7 +4008,7 @@
diff --git a/public/script.js b/public/script.js index b5d38d6d3..e0e0f2ab9 100644 --- a/public/script.js +++ b/public/script.js @@ -150,6 +150,7 @@ import { humanFileSize, Stopwatch, isValidUrl, + ensureImageFormatSupported, } from './scripts/utils.js'; import { ModuleWorkerWrapper, doDailyExtensionUpdatesCheck, extension_settings, getContext, loadExtensionSettings, renderExtensionTemplate, runGenerationInterceptors, saveMetadataDebounced, writeExtensionField } from './scripts/extensions.js'; @@ -5713,6 +5714,12 @@ async function uploadUserAvatar(e) { } } + const rawFile = formData.get('avatar'); + if (rawFile instanceof File){ + const convertedFile = await ensureImageFormatSupported(rawFile); + formData.set('avatar', convertedFile); + } + jQuery.ajax({ type: 'POST', url: url, @@ -7212,8 +7219,15 @@ function addAlternateGreeting(template, greeting, index, getArray) { async function createOrEditCharacter(e) { $('#rm_info_avatar').html(''); - var formData = new FormData($('#form_create').get(0)); + const formData = new FormData($('#form_create').get(0)); formData.set('fav', fav_ch_checked); + + const rawFile = formData.get('avatar'); + if (rawFile instanceof File){ + const convertedFile = await ensureImageFormatSupported(rawFile); + formData.set('avatar', convertedFile); + } + if ($('#form_create').attr('actiontype') == 'createcharacter') { if ($('#character_name_pole').val().length > 0) { if (is_group_generating || is_send_press) { diff --git a/public/scripts/utils.js b/public/scripts/utils.js index 4f825d4b2..0b309c4f0 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -1047,15 +1047,51 @@ export function loadFileToDocument(url, type) { }); } +/** + * Ensure that we can import war crime image formats like WEBP and AVIF. + * @param {File} file Input file + * @returns {Promise} A promise that resolves to the supported file. + */ +export async function ensureImageFormatSupported(file) { + const supportedTypes = [ + 'image/jpeg', + 'image/png', + 'image/bmp', + 'image/tiff', + 'image/gif', + 'image/apng', + ]; + + if (supportedTypes.includes(file.type) || !file.type.startsWith('image/')) { + return file; + } + + return await convertImageFile(file, 'image/png'); +} + +/** + * Converts an image file to a given format. + * @param {File} inputFile File to convert + * @param {string} type Target file type + * @returns {Promise} A promise that resolves to the converted file. + */ +export async function convertImageFile(inputFile, type = 'image/png') { + const base64 = await getBase64Async(inputFile); + const thumbnail = await createThumbnail(base64, null, null, type); + const blob = await fetch(thumbnail).then(res => res.blob()); + const outputFile = new File([blob], inputFile.name, { type }); + return outputFile; +} + /** * Creates a thumbnail from a data URL. * @param {string} dataUrl The data URL encoded data of the image. - * @param {number} maxWidth The maximum width of the thumbnail. - * @param {number} maxHeight The maximum height of the thumbnail. + * @param {number|null} maxWidth The maximum width of the thumbnail. + * @param {number|null} maxHeight The maximum height of the thumbnail. * @param {string} [type='image/jpeg'] The type of the thumbnail. * @returns {Promise} A promise that resolves to the thumbnail data URL. */ -export function createThumbnail(dataUrl, maxWidth, maxHeight, type = 'image/jpeg') { +export function createThumbnail(dataUrl, maxWidth = null, maxHeight = null, type = 'image/jpeg') { // Someone might pass in a base64 encoded string without the data URL prefix if (!dataUrl.includes('data:')) { dataUrl = `data:image/jpeg;base64,${dataUrl}`; @@ -1073,6 +1109,16 @@ export function createThumbnail(dataUrl, maxWidth, maxHeight, type = 'image/jpeg let thumbnailWidth = maxWidth; let thumbnailHeight = maxHeight; + if (maxWidth === null) { + thumbnailWidth = img.width; + maxWidth = img.width; + } + + if (maxHeight === null) { + thumbnailHeight = img.height; + maxHeight = img.height; + } + if (img.width > img.height) { thumbnailHeight = maxWidth / aspectRatio; } else {