gif support

This commit is contained in:
loicbersier 2020-02-21 21:13:03 +01:00
parent c70988384e
commit 48acabf3a6

View file

@ -41,8 +41,6 @@ class memeCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
let output = `${os.tmpdir()}/meme${message.id}.jpg`;
let options = args.message.trim().split('|'); let options = args.message.trim().split('|');
if (options[0] == undefined) if (options[0] == undefined)
@ -62,11 +60,11 @@ class memeCommand extends Command {
// Create new graphicsmagick instance // Create new graphicsmagick instance
fetch(url) fetch(url)
.then(res => { .then(res => {
const dest = fs.createWriteStream(`${os.tmpdir()}/${message.id}.jpg`); const dest = fs.createWriteStream(`${os.tmpdir()}/${message.id}`);
res.body.pipe(dest); res.body.pipe(dest);
dest.on('finish', async () => { dest.on('finish', async () => {
let img = gm(`${os.tmpdir()}/${message.id}.jpg`); let img = gm(`${os.tmpdir()}/${message.id}`);
// Set some defaults // Set some defaults
const TOP_TEXT = options[0]; const TOP_TEXT = options[0];
@ -78,26 +76,36 @@ class memeCommand extends Command {
const STROKE_WEIGHT = 2; const STROKE_WEIGHT = 2;
const PADDING = 40; const PADDING = 40;
// Get the image size to calculate top and bottom text positions img.format(function(err, format) {
img.size(function(err, value) { if (err) {
console.log(value); return message.channel.send('An error has occured, is it an image?');
// Set text position for top and bottom }
const TOP_POS = Math.abs((value.height / 2) - PADDING) * -1; let output = `${os.tmpdir()}/meme${message.id}.${format.toLocaleLowerCase()}`;
const BOTTOM_POS = (value.height / 2) - PADDING; // Get the image size to calculate top and bottom text positions
let FONT_SIZE = args.fontSize ? args.fontSize : (value.width / 10); img.size(function(err, value) {
// Set text position for top and bottom
// Write text on image using graphicsmagick const TOP_POS = Math.abs((value.height / 2) - PADDING) * -1;
img.font(FONT, FONT_SIZE) const BOTTOM_POS = (value.height / 2) - PADDING;
.fill(FONT_FILL) let FONT_SIZE = args.fontSize ? args.fontSize : (value.width / 10);
.stroke(STROKE_COLOR, STROKE_WEIGHT)
.drawText(0, TOP_POS, TOP_TEXT, TEXT_POS) // Write text on image using graphicsmagick
.drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS) img.font(FONT, FONT_SIZE)
.write(output, function(err) { .fill(FONT_FILL)
loadingmsg.delete(); .stroke(STROKE_COLOR, STROKE_WEIGHT)
if (err) return message.channel.send('An error just occured! is it a static image?'); .drawText(0, TOP_POS, TOP_TEXT, TEXT_POS)
return message.channel.send({files: [output]}); .drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS)
}); .write(output, function(err) {
loadingmsg.delete();
if (err) return message.channel.send('An error just occured! is it a static image?');
message.delete();
return message.channel.send(`Made by ${message.author.username}`,{files: [output]})
.catch(() => {
return message.channel.send('The image is too big to fit on discord!');
});
});
});
}); });
}); });
}) })
.catch((err) => { .catch((err) => {