fix(img2gif): keep original aspect ratio and filename (#487)
* fix(img2gif): keep original aspect ratio and filename * remove contribution
This commit is contained in:
parent
f8b65055a1
commit
9c3ab5d9dc
1 changed files with 18 additions and 3 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue