From c98a572a02657499033e4f997b1d1423e8a01bab Mon Sep 17 00:00:00 2001 From: loicbersier Date: Fri, 21 Feb 2020 18:33:53 +0100 Subject: [PATCH] Gif support --- commands/images/poster.js | 92 ++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/commands/images/poster.js b/commands/images/poster.js index d49b691a..bafca8ba 100644 --- a/commands/images/poster.js +++ b/commands/images/poster.js @@ -36,8 +36,6 @@ class posterCommand extends Command { } async exec(message, args) { - let output = `${os.tmpdir()}/poster${message.id}.jpg`; - let options = args.message.trim().split('|'); if (options[0] == undefined) @@ -57,11 +55,11 @@ class posterCommand extends Command { // Create new graphicsmagick instance fetch(url) .then(res => { - const dest = fs.createWriteStream(`${os.tmpdir()}/${message.id}.jpg`); + const dest = fs.createWriteStream(`${os.tmpdir()}/${message.id}`); res.body.pipe(dest); dest.on('finish', async () => { - let img = gm(`${os.tmpdir()}/${message.id}.jpg`); + let img = gm(`${os.tmpdir()}/${message.id}`); // Set some defaults const TOP_TEXT = options[0]; @@ -71,49 +69,53 @@ class posterCommand extends Command { const TEXT_POS = 'center'; const STROKE_COLOR = '#000'; const PADDING = 40; - - // Get the image size to calculate top and bottom text positions - img.size(function(err, value) { - // Set text position for top and bottom - const TOP_POS = Math.abs((value.height / 2) - PADDING + 110); - const BOTTOM_POS = Math.abs((value.height / 2) - PADDING + 180); - - const FONT_SIZE1 = (value.width / 12); - const FONT_SIZE2 = (value.width / 12) - 15; - - // Write text on image using graphicsmagick - img.borderColor('black') - .border(3,3) - .borderColor('white') - .frame(1,1,0,0.5) - .border(1,1) - .borderColor('black') - .border(50,200) - .fill(FONT_FILL) - .stroke(STROKE_COLOR, -2) - .font(FONT, FONT_SIZE1) - .drawText(0, TOP_POS, TOP_TEXT, TEXT_POS) - .font(FONT, FONT_SIZE2) - .drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS) - .write(output, function(err) { - // Chop the top part of the image - let img2 = gm(output); - img2.chop(0, 100) - .write(output, function(err) { - loadingmsg.delete(); - if (err) { - console.error(err); - return message.channel.send('An error just occured! is it a static image?'); - } - return message.channel.send({files: [output]}); - }); - if (err) { - console.error(err); - return message.channel.send('An error just occured! is it a static image?'); - } - }); + img.format(function(err, format) { + let output = `${os.tmpdir()}/poster${message.id}.${format.toLocaleLowerCase()}`; + // Get the image size to calculate top and bottom text positions + img.size(function(err, value) { + // Set text position for top and bottom + const TOP_POS = Math.abs((value.height / 2) - PADDING + 110); + const BOTTOM_POS = Math.abs((value.height / 2) - PADDING + 180); + + const FONT_SIZE1 = (value.width / 12); + const FONT_SIZE2 = (value.width / 12) - 15; + + // Write text on image using graphicsmagick + img.borderColor('black') + .border(3,3) + .borderColor('white') + .frame(1,1,0,0.5) + .border(1,1) + .borderColor('black') + .border(50,200) + .fill(FONT_FILL) + .stroke(STROKE_COLOR, -2) + .font(FONT, FONT_SIZE1) + .drawText(0, TOP_POS, TOP_TEXT, TEXT_POS) + .font(FONT, FONT_SIZE2) + .drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS) + .write(output, function(err) { + // Chop the top part of the image + let img2 = gm(output); + img2.chop(0, 100) + .write(output, function(err) { + loadingmsg.delete(); + if (err) { + console.error(err); + return message.channel.send('An error just occured! is it a static image?'); + } + return message.channel.send({files: [output]}); + }); + + if (err) { + console.error(err); + return message.channel.send('An error just occured! is it a static image?'); + } + }); + }); }); + }); }) .catch((err) => {