1
0
Fork 0

Gif support

akairo
loicbersier 4 years ago
parent 1ec9fa887f
commit c98a572a02

@ -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 FONT_SIZE1 = (value.width / 12); const BOTTOM_POS = Math.abs((value.height / 2) - PADDING + 180);
const FONT_SIZE2 = (value.width / 12) - 15;
const FONT_SIZE1 = (value.width / 12);
// Write text on image using graphicsmagick const FONT_SIZE2 = (value.width / 12) - 15;
img.borderColor('black')
.border(3,3) // Write text on image using graphicsmagick
.borderColor('white') img.borderColor('black')
.frame(1,1,0,0.5) .border(3,3)
.border(1,1) .borderColor('white')
.borderColor('black') .frame(1,1,0,0.5)
.border(50,200) .border(1,1)
.fill(FONT_FILL) .borderColor('black')
.stroke(STROKE_COLOR, -2) .border(50,200)
.font(FONT, FONT_SIZE1) .fill(FONT_FILL)
.drawText(0, TOP_POS, TOP_TEXT, TEXT_POS) .stroke(STROKE_COLOR, -2)
.font(FONT, FONT_SIZE2) .font(FONT, FONT_SIZE1)
.drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS) .drawText(0, TOP_POS, TOP_TEXT, TEXT_POS)
.write(output, function(err) { .font(FONT, FONT_SIZE2)
// Chop the top part of the image .drawText(0, BOTTOM_POS, BOTTOM_TEXT, TEXT_POS)
let img2 = gm(output); .write(output, function(err) {
img2.chop(0, 100) // Chop the top part of the image
.write(output, function(err) { let img2 = gm(output);
loadingmsg.delete(); img2.chop(0, 100)
if (err) { .write(output, function(err) {
console.error(err); loadingmsg.delete();
return message.channel.send('An error just occured! is it a static image?'); if (err) {
} console.error(err);
return message.channel.send({files: [output]}); 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?'); if (err) {
} console.error(err);
}); return message.channel.send('An error just occured! is it a static image?');
}
});
});
}); });
}); });
}) })
.catch((err) => { .catch((err) => {

Loading…
Cancel
Save