Make squish use imagemagick

This commit is contained in:
Dragon Fire
2020-06-30 15:26:15 -04:00
parent 215e518847
commit 8a40b24cd6
2 changed files with 21 additions and 10 deletions
+20 -9
View File
@@ -1,12 +1,12 @@
const Command = require('../../structures/Command');
const { createCanvas, loadImage } = require('canvas');
const gm = require('gm').subClass({ imageMagick: true });
const request = require('node-superfetch');
module.exports = class SquishCommand extends Command {
constructor(client) {
super(client, {
name: 'squish',
aliases: ['stretch'],
aliases: ['stretch', 'flatten', 'flat'],
group: 'edit-image',
memberName: 'squish',
description: 'Draws an image or a user\'s avatar but squished across the X or Y axis.',
@@ -34,19 +34,30 @@ module.exports = class SquishCommand extends Command {
}
async run(msg, { axis, image }) {
const flipX = axis === 'x';
const flipY = axis === 'y';
let command;
if (axis === 'x') command = '15%x100%';
if (axis === 'y') command = '100%x15%';
try {
const { body } = await request.get(image);
const data = await loadImage(body);
const canvas = createCanvas(flipX ? data.width / 2 : data.width, flipY ? data.height / 2 : data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(data, 0, 0, canvas.width, canvas.height);
const attachment = canvas.toBuffer();
const magik = gm(body);
magik.out('-liquid-rescale');
magik.out(command);
magik.setFormat('png');
const attachment = await this.toBuffer(magik);
if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.');
return msg.say({ files: [{ attachment, name: 'squish.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
toBuffer(magik) {
return new Promise((res, rej) => {
magik.toBuffer((err, buffer) => {
if (err) return rej(err);
return res(buffer);
});
});
}
};
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "117.8.0",
"version": "117.8.1",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {