From c70f7dee5ed61f19ab6c86def381a217d3c4a274 Mon Sep 17 00:00:00 2001 From: TheEssem Date: Wed, 8 Jan 2020 19:10:26 -0600 Subject: [PATCH] Modified image commands to work completely with animated GIF files, fixed oversight with caption and help --- commands/9gag.js | 2 +- commands/bandicam.js | 2 +- commands/caption.js | 1 + commands/deviantart.js | 2 +- commands/flip.js | 8 ++++++-- commands/flop.js | 8 ++++++-- commands/funky.js | 2 +- commands/gamexplain.js | 6 +++--- commands/haah.js | 9 ++++++--- commands/help.js | 2 +- commands/hooh.js | 9 ++++++--- commands/hypercam.js | 2 +- commands/ifunny.js | 2 +- commands/leak.js | 2 ++ commands/magik.js | 1 + commands/memecenter.js | 4 ++-- commands/scott.js | 6 +++--- commands/shutterstock.js | 2 +- commands/trump.js | 6 +++--- commands/waaw.js | 9 ++++++--- commands/wdt.js | 6 +++--- commands/woow.js | 9 ++++++--- 22 files changed, 62 insertions(+), 38 deletions(-) diff --git a/commands/9gag.js b/commands/9gag.js index ec2b025..e9a4cff 100644 --- a/commands/9gag.js +++ b/commands/9gag.js @@ -8,7 +8,7 @@ exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a 9GAG watermark!`; const watermark = "./assets/images/9gag.png"; - const data = gm(image.data).composite(watermark).gravity("East"); + const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("East").out("-layers", "composite"); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/bandicam.js b/commands/bandicam.js index e7200d4..664ecc1 100644 --- a/commands/bandicam.js +++ b/commands/bandicam.js @@ -10,7 +10,7 @@ exports.run = async (message) => { const watermark = "./assets/images/bandicam.png"; gm(image.data).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).composite(watermark).gravity("North").resize(null, size.height); + const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("North").resize(null, size.height).out("-layers", "composite"); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/caption.js b/commands/caption.js index 976983d..fbcc468 100644 --- a/commands/caption.js +++ b/commands/caption.js @@ -7,6 +7,7 @@ const gmToBuffer = require("../utils/gmbuffer.js"); exports.run = async (message, args) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image/GIF to add a caption!`; + if (args.length === 0) return `${message.author.mention}, you need to provide some text to add a caption!`; const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const processMessage = await message.channel.createMessage(" Processing... This might take a while"); require("util").promisify(fs.writeFile)(path, image.data); diff --git a/commands/deviantart.js b/commands/deviantart.js index 997a791..8dd881b 100644 --- a/commands/deviantart.js +++ b/commands/deviantart.js @@ -10,7 +10,7 @@ exports.run = async (message) => { const watermark = "./assets/images/deviantart.png"; gm(image.data).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).composite(watermark).gravity("Center").resize(null, size.height); + const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite"); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/flip.js b/commands/flip.js index b9e4c05..ad28c73 100644 --- a/commands/flip.js +++ b/commands/flip.js @@ -1,10 +1,14 @@ -const sharp = require("sharp"); +const gm = require("gm").subClass({ + imageMagick: true +}); +const gmToBuffer = require("../utils/gmbuffer.js"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to flip!`; - const resultBuffer = await sharp(image.data).flip().toBuffer(); + const command = gm(image.data).flip(); + const resultBuffer = await gmToBuffer(command); return message.channel.createMessage("", { file: resultBuffer, name: `flip.${image.type}` diff --git a/commands/flop.js b/commands/flop.js index d11c304..356f0d3 100644 --- a/commands/flop.js +++ b/commands/flop.js @@ -1,10 +1,14 @@ -const sharp = require("sharp"); +const gm = require("gm").subClass({ + imageMagick: true +}); +const gmToBuffer = require("../utils/gmbuffer.js"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to flop!`; - const resultBuffer = await sharp(image.data).flop().toBuffer(); + const command = gm(image.data).flop(); + const resultBuffer = await gmToBuffer(command); return message.channel.createMessage("", { file: resultBuffer, name: `flop.${image.type}` diff --git a/commands/funky.js b/commands/funky.js index 2def89f..1cc257e 100644 --- a/commands/funky.js +++ b/commands/funky.js @@ -10,7 +10,7 @@ exports.run = async (message) => { const watermark = "./assets/images/funky.png"; gm(image.data).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).composite(watermark).gravity("NorthEast").resize(null, size.height); + const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("NorthEast").resize(null, size.height).out("-layers", "composite"); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/gamexplain.js b/commands/gamexplain.js index fc9d73f..0568a49 100644 --- a/commands/gamexplain.js +++ b/commands/gamexplain.js @@ -11,11 +11,11 @@ exports.run = async (message) => { const template = "./assets/images/gamexplain.png"; const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; require("util").promisify(fs.writeFile)(path, image.data); - const command = gm(template).out("-background").out("white").out("-gravity").out("Center").out("(").out("-clone").out("0").out("(").out(path).out("-virtual-pixel").out("transparent").out("-resize").out("1181x571!").out(")").out("-geometry").out("+0+40").out("-composite").out(")").out("+swap").out("-composite"); - const resultBuffer = await gmToBuffer(command); + const command = gm(template).background("white").out("null:").out("(").out(path).out("-virtual-pixel", "transparent").resize("1181x571!").coalesce().out(")").compose("over").gravity("Center").out("-geometry", "+0+40").out("-layers", "composite"); + const resultBuffer = await gmToBuffer(command, image.type); return message.channel.createMessage("", { file: resultBuffer, - name: "gamexplain.png" + name: `gamexplain.${image.type}` }); }; diff --git a/commands/haah.js b/commands/haah.js index a9722c1..2fa6403 100644 --- a/commands/haah.js +++ b/commands/haah.js @@ -3,6 +3,7 @@ const gm = require("gm").subClass({ imageMagick: true }); +const gmToBuffer = require("../utils/gmbuffer.js"); exports.run = async (message) => { message.channel.sendTyping(); @@ -10,12 +11,14 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to mirror!`; const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - gm(image.data).gravity("West").crop("50%", 0).strip().write(data2, (error) => { + gm(image.data).size((error, size) => { if (error) throw error; - gm(data2).flop().strip().write(data, async (error) => { + gm(image.data).gravity("West").crop("50%", 0).out("+repage").write(data2, (error) => { if (error) throw error; - gm(data2).append(data, true).toBuffer(image.type, (error, resultBuffer) => { + gm(data2).flop().write(data, async (error) => { if (error) throw error; + const command = gm(data2).out("-repage", `${size.width}x${size.height}`).coalesce().out("null:").out("(").out(data).coalesce().out(")").geometry(`+${size.width / 2}+0`).out("-layers", "Composite"); + const resultBuffer = await gmToBuffer(command); return message.channel.createMessage("", { file: resultBuffer, name: `haah.${image.type}` diff --git a/commands/help.js b/commands/help.js index 7a1c004..a73d35e 100644 --- a/commands/help.js +++ b/commands/help.js @@ -36,7 +36,7 @@ exports.run = async (message, args) => { moderation: [], tags: ["**Every command in this category is a subcommand of the tag command.**\n"], fun: [], - images: ["**These commands support the PNG, JPEG, and WEBP formats.**\n"], + images: ["**These commands support the PNG, JPEG, WEBP, and GIF formats. (GIF support is experimental)**\n"], soundboard: [], admin: ["**These commands are only available to the bot owner.**\n"] }; diff --git a/commands/hooh.js b/commands/hooh.js index dd590bf..f2cae0e 100644 --- a/commands/hooh.js +++ b/commands/hooh.js @@ -3,6 +3,7 @@ const gm = require("gm").subClass({ imageMagick: true }); +const gmToBuffer = require("../utils/gmbuffer.js"); exports.run = async (message) => { message.channel.sendTyping(); @@ -10,12 +11,14 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to mirror!`; const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - gm(image.data).gravity("South").crop(0, "50%").strip().write(data2, (error) => { + gm(image.data).size((error, size) => { if (error) throw error; - gm(data2).flip().strip().write(data, async (error) => { + gm(image.data).gravity("South").crop(0, "50%").out("+repage").write(data2, (error) => { if (error) throw error; - gm(data).append(data2).toBuffer(image.type, (error, resultBuffer) => { + gm(data2).flip().write(data, async (error) => { if (error) throw error; + const command = gm(data).out("-repage", `${size.width}x${size.height}`).coalesce().out("null:").out("(").out(data2).coalesce().out(")").geometry(`+0+${size.height / 2}`).out("-layers", "Composite"); + const resultBuffer = await gmToBuffer(command); return message.channel.createMessage("", { file: resultBuffer, name: `hooh.${image.type}` diff --git a/commands/hypercam.js b/commands/hypercam.js index efa9f69..eb76b71 100644 --- a/commands/hypercam.js +++ b/commands/hypercam.js @@ -10,7 +10,7 @@ exports.run = async (message) => { const watermark = "./assets/images/hypercam.png"; gm(image.data).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).composite(watermark).gravity("NorthWest").resize(null, size.height); + const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("NorthWest").resize(null, size.height).out("-layers", "composite"); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/ifunny.js b/commands/ifunny.js index eedbb06..6b62935 100644 --- a/commands/ifunny.js +++ b/commands/ifunny.js @@ -10,7 +10,7 @@ exports.run = async (message) => { const watermark = "./assets/images/ifunny.png"; gm(image.data).size(async (error, size) => { if (error) throw error; - const data = gm(image.data).append(watermark).gravity("South").resize(size.width, null); + const data = gm(image.data).coalesce().out("null:").out(watermark).gravity("South").resize(size.width, null).out("-layers", "composite"); const resultBuffer = await gmToBuffer(data); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/leak.js b/commands/leak.js index 38961ee..7ab1084 100644 --- a/commands/leak.js +++ b/commands/leak.js @@ -8,10 +8,12 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to make a Super Smash Bros. leak meme!`; + if (image.type === "gif") return `${message.author.mention}, this command doesn't work with GIFs!`; const template = "./assets/images/leak.png"; const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; require("util").promisify(fs.writeFile)(path, image.data); const command = gm(template).out("-background").out("white").out("-gravity").out("Center").out("(").out("-clone").out("0").out("(").out(path).out("-virtual-pixel").out("white").out("-resize").out("640x360!").rotate("white", 15).out(")").out("-geometry").out("+450-200").out("-composite").out(")").out("+swap").out("-composite").out("-alpha").out("remove").out("-alpha").out("off"); + // const command = gm(template).out("-background", "white").gravity("Center").out("null:").out("(").out(path).out("-resize", "640x360!").out("-virtual-pixel", "white").rotate("white", 15).coalesce().geometry("+450-200").out(")").compose("over").out("-alpha", "remove").out("-alpha", "off").out("-layers", "composite"); const resultBuffer = await gmToBuffer(command, "png"); return message.channel.createMessage("", { file: resultBuffer, diff --git a/commands/magik.js b/commands/magik.js index 0e6b04d..3c9178b 100644 --- a/commands/magik.js +++ b/commands/magik.js @@ -6,6 +6,7 @@ const gmToBuffer = require("../utils/gmbuffer.js"); exports.run = async (message) => { const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add some magik!`; + if (image.type === "gif") return `${message.author.mention}, this command doesn't work with GIFs!`; const processMessage = await message.channel.createMessage(" Processing... This might take a while"); gm(image.data).resize(800, 800).stream((error, stream) => { if (error) throw error; diff --git a/commands/memecenter.js b/commands/memecenter.js index 670c292..d8bd643 100644 --- a/commands/memecenter.js +++ b/commands/memecenter.js @@ -11,8 +11,8 @@ exports.run = async (message) => { let resultBuffer; gm(image.data).size(async (error, size) => { if (error) throw error; - const command = gm(image.data).out(watermark).background("#FFFFFF").gravity("East").out("-smush").out("-9"); - const output = await gmToBuffer(command, "png"); + const command = gm(image.data).coalesce().background("white").extent(size.width, size.height + 15).out("null:").out(watermark).gravity("SouthEast").compose("over").out("-layers", "composite"); + const output = await gmToBuffer(command, image.type); gm(output).size(async (error, size2) => { if (error) throw error; resultBuffer = output; diff --git a/commands/scott.js b/commands/scott.js index 6af27d6..b6b8870 100644 --- a/commands/scott.js +++ b/commands/scott.js @@ -11,11 +11,11 @@ exports.run = async (message) => { const template = "./assets/images/scott.png"; const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; require("util").promisify(fs.writeFile)(path, image.data); - const command = gm(template).out("-gravity").out("Center").out("(").out(path).out("-virtual-pixel").out("transparent").out("-resize").out("415x234!").out("+distort").out("Perspective").out("0,0 129,187 415,0 517,182 415,234 517,465 0,234 132,418").out("-geometry").out("-110+83").out(")").out("-composite"); - const resultBuffer = await gmToBuffer(command, "png"); + const command = gm(template).out("null:").out("(").out(path).out("-virtual-pixel", "transparent").resize("415x234!").coalesce().out("+distort", "Perspective", "0,0 129,187 415,0 517,182 415,234 517,465 0,234 132,418").out(")").compose("over").gravity("Center").geometry("-238-98").out("-layers", "composite"); + const resultBuffer = await gmToBuffer(command, image.type); return message.channel.createMessage("", { file: resultBuffer, - name: "scott.png" + name: `scott.${image.type}` }); }; diff --git a/commands/shutterstock.js b/commands/shutterstock.js index 4426f39..f7ab5f4 100644 --- a/commands/shutterstock.js +++ b/commands/shutterstock.js @@ -10,7 +10,7 @@ exports.run = async (message) => { const watermark = "./assets/images/shutterstock.png"; gm(image.data).size(async (error, size) => { if (error) throw error; - const command = gm(image.data).composite(watermark).gravity("Center").resize(null, size.height); + const command = gm(image.data).coalesce().out("null:").out(watermark).gravity("Center").resize(null, size.height).out("-layers", "composite"); const output = await gmToBuffer(command); return message.channel.createMessage("", { file: output, diff --git a/commands/trump.js b/commands/trump.js index 3f25d63..f615440 100644 --- a/commands/trump.js +++ b/commands/trump.js @@ -11,11 +11,11 @@ exports.run = async (message) => { const template = "./assets/images/trump.png"; const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; require("util").promisify(fs.writeFile)(path, image.data); - const command = gm(template).out("-background").out("none").out("-gravity").out("South").out("(").out("-clone").out("0").out("(").out(path).out("-virtual-pixel").out("transparent").out("-resize").out("365x179!").out("+distort").out("Perspective").out("0,0 207,268 365,0 548,271 365,179 558,450 0,179 193,450").out(")").out("-geometry").out("-25-1").out("-composite").out(")").out("+swap").out("-composite"); - const resultBuffer = await gmToBuffer(command, "png"); + const command = gm(template).background("white").out("null:").out("(").out(path).out("-virtual-pixel", "transparent").resize("365x179!").coalesce().out("+distort", "Perspective", "0,0 207,268 365,0 548,271 365,179 558,450 0,179 193,450").out(")").compose("over").gravity("Center").geometry("-217-135").out("-layers", "composite"); + const resultBuffer = await gmToBuffer(command, image.type); return message.channel.createMessage("", { file: resultBuffer, - name: "trump.png" + name: `trump.${image.type}` }); }; diff --git a/commands/waaw.js b/commands/waaw.js index b5b86e0..8a46643 100644 --- a/commands/waaw.js +++ b/commands/waaw.js @@ -3,6 +3,7 @@ const gm = require("gm").subClass({ imageMagick: true }); +const gmToBuffer = require("../utils/gmbuffer.js"); exports.run = async (message) => { message.channel.sendTyping(); @@ -10,12 +11,14 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to mirror!`; const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - gm(image.data).gravity("East").crop("50%", 0).strip().write(data2, (error) => { + gm(image.data).size((error, size) => { if (error) throw error; - gm(data2).flop().strip().write(data, async (error) => { + gm(image.data).gravity("East").crop("50%", 0).out("+repage").write(data2, (error) => { if (error) throw error; - gm(data).append(data2, true).toBuffer(image.type, (error, resultBuffer) => { + gm(data2).flop().write(data, async (error) => { if (error) throw error; + const command = gm(data).out("-repage", `${size.width}x${size.height}`).coalesce().out("null:").out("(").out(data2).coalesce().out(")").geometry(`+${size.width / 2}+0`).out("-layers", "Composite"); + const resultBuffer = await gmToBuffer(command); return message.channel.createMessage("", { file: resultBuffer, name: `waaw.${image.type}` diff --git a/commands/wdt.js b/commands/wdt.js index b863f7c..4720ddc 100644 --- a/commands/wdt.js +++ b/commands/wdt.js @@ -11,11 +11,11 @@ exports.run = async (message) => { const template = "./assets/images/whodidthis.png"; const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; require("util").promisify(fs.writeFile)(path, image.data); - const command = gm(template).composite(path).gravity("Center").geometry("374x374+0+0"); - const resultBuffer = await gmToBuffer(command, "png"); + const command = gm(template).coalesce().out("null:").out(path).gravity("Center").resize("374x374>").out("-layers", "composite"); + const resultBuffer = await gmToBuffer(command, image.type); return message.channel.createMessage("", { file: resultBuffer, - name: "wdt.png" + name: `wdt.${image.type}` }); }; diff --git a/commands/woow.js b/commands/woow.js index 6f88c68..769902e 100644 --- a/commands/woow.js +++ b/commands/woow.js @@ -3,6 +3,7 @@ const gm = require("gm").subClass({ imageMagick: true }); +const gmToBuffer = require("../utils/gmbuffer.js"); exports.run = async (message) => { message.channel.sendTyping(); @@ -10,12 +11,14 @@ exports.run = async (message) => { if (image === undefined) return `${message.author.mention}, you need to provide an image to mirror!`; const data = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; const data2 = `/tmp/${Math.random().toString(36).substring(2, 15)}.${image.type}`; - gm(image.data).gravity("North").crop(0, "50%").strip().write(data2, (error) => { + gm(image.data).size((error, size) => { if (error) throw error; - gm(data2).flip().strip().write(data, async (error) => { + gm(image.data).gravity("North").crop(0, "50%").out("+repage").write(data2, (error) => { if (error) throw error; - gm(data2).append(data).toBuffer(image.type, (error, resultBuffer) => { + gm(data2).flip().write(data, async (error) => { if (error) throw error; + const command = gm(data2).out("-repage", `${size.width}x${size.height}`).coalesce().out("null:").out("(").out(data).coalesce().out(")").geometry(`+0+${size.height / 2}`).out("-layers", "Composite"); + const resultBuffer = await gmToBuffer(command); return message.channel.createMessage("", { file: resultBuffer, name: `woow.${image.type}`