fix(img2gif): keep original aspect ratio and filename (#487)

* fix(img2gif): keep original aspect ratio and filename

* remove contribution
This commit is contained in:
neo 2025-11-09 09:11:43 -05:00 • committed by GitHub
parent f8b65055a1
commit 9c3ab5d9dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -108,8 +108,22 @@ export default definePlugin({
const avatar = await loadImage(image);
const gifHeight = height ?? DEFAULT_RESOLUTION;
const gifWidth = width ?? DEFAULT_RESOLUTION;
let gifWidth: number;
let gifHeight: number;
if (width && height) {
gifWidth = width;
gifHeight = height;
} else if (width) {
gifWidth = width;
gifHeight = Math.round((avatar.height / avatar.width) * width);
} else if (height) {
gifHeight = height;
gifWidth = Math.round((avatar.width / avatar.height) * height);
} else {
gifWidth = avatar.width;
gifHeight = avatar.height;
}
const gif = GIFEncoder();
const canvas = document.createElement("canvas");
@ -134,7 +148,8 @@ export default definePlugin({
}
gif.finish();
const file = new File([gif.bytesView()], "converted.gif", { type: "image/gif" });
const originalName = image.name ? image.name.replace(/\.[^/.]+$/, "") : "converted";
const file = new File([new Uint8Array(gif.bytesView())], `${originalName}.gif`, { type: "image/gif" });
setTimeout(() => UploadHandler.promptToUpload([file], cmdCtx.channel, DraftType.ChannelMessage), 10);
} catch (err) {
UploadManager.clearAll(cmdCtx.channel.id, DraftType.SlashCommand);