Gif support

This commit is contained in:
loicbersier 2020-02-21 18:33:53 +01:00
parent 1ec9fa887f
commit c98a572a02

View file

@ -36,8 +36,6 @@ class posterCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
let output = `${os.tmpdir()}/poster${message.id}.jpg`;
let options = args.message.trim().split('|'); let options = args.message.trim().split('|');
if (options[0] == undefined) if (options[0] == undefined)
@ -57,11 +55,11 @@ class posterCommand 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];
@ -72,48 +70,52 @@ class posterCommand extends Command {
const STROKE_COLOR = '#000'; const STROKE_COLOR = '#000';
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) { let output = `${os.tmpdir()}/poster${message.id}.${format.toLocaleLowerCase()}`;
// Set text position for top and bottom // Get the image size to calculate top and bottom text positions
const TOP_POS = Math.abs((value.height / 2) - PADDING + 110); img.size(function(err, value) {
const BOTTOM_POS = Math.abs((value.height / 2) - PADDING + 180); // 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_SIZE1 = (value.width / 12);
const FONT_SIZE2 = (value.width / 12) - 15; const FONT_SIZE2 = (value.width / 12) - 15;
// Write text on image using graphicsmagick // Write text on image using graphicsmagick
img.borderColor('black') img.borderColor('black')
.border(3,3) .border(3,3)
.borderColor('white') .borderColor('white')
.frame(1,1,0,0.5) .frame(1,1,0,0.5)
.border(1,1) .border(1,1)
.borderColor('black') .borderColor('black')
.border(50,200) .border(50,200)
.fill(FONT_FILL) .fill(FONT_FILL)
.stroke(STROKE_COLOR, -2) .stroke(STROKE_COLOR, -2)
.font(FONT, FONT_SIZE1) .font(FONT, FONT_SIZE1)
.drawText(0, TOP_POS, TOP_TEXT, TEXT_POS) .drawText(0, TOP_POS, TOP_TEXT, TEXT_POS)
.font(FONT, FONT_SIZE2) .font(FONT, FONT_SIZE2)
.drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS) .drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS)
.write(output, function(err) { .write(output, function(err) {
// Chop the top part of the image // Chop the top part of the image
let img2 = gm(output); let img2 = gm(output);
img2.chop(0, 100) img2.chop(0, 100)
.write(output, function(err) { .write(output, function(err) {
loadingmsg.delete(); loadingmsg.delete();
if (err) { if (err) {
console.error(err); console.error(err);
return message.channel.send('An error just occured! is it a static image?'); return message.channel.send('An error just occured! is it a static image?');
} }
return message.channel.send({files: [output]}); return message.channel.send({files: [output]});
}); });
if (err) { if (err) {
console.error(err); console.error(err);
return message.channel.send('An error just occured! is it a static image?'); return message.channel.send('An error just occured! is it a static image?');
} }
}); });
});
}); });
}); });
}) })
.catch((err) => { .catch((err) => {