mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-18 21:40:49 +02:00
Make squish use imagemagick
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
const Command = require('../../structures/Command');
|
const Command = require('../../structures/Command');
|
||||||
const { createCanvas, loadImage } = require('canvas');
|
const gm = require('gm').subClass({ imageMagick: true });
|
||||||
const request = require('node-superfetch');
|
const request = require('node-superfetch');
|
||||||
|
|
||||||
module.exports = class SquishCommand extends Command {
|
module.exports = class SquishCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
super(client, {
|
super(client, {
|
||||||
name: 'squish',
|
name: 'squish',
|
||||||
aliases: ['stretch'],
|
aliases: ['stretch', 'flatten', 'flat'],
|
||||||
group: 'edit-image',
|
group: 'edit-image',
|
||||||
memberName: 'squish',
|
memberName: 'squish',
|
||||||
description: 'Draws an image or a user\'s avatar but squished across the X or Y axis.',
|
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 }) {
|
async run(msg, { axis, image }) {
|
||||||
const flipX = axis === 'x';
|
let command;
|
||||||
const flipY = axis === 'y';
|
if (axis === 'x') command = '15%x100%';
|
||||||
|
if (axis === 'y') command = '100%x15%';
|
||||||
try {
|
try {
|
||||||
const { body } = await request.get(image);
|
const { body } = await request.get(image);
|
||||||
const data = await loadImage(body);
|
const magik = gm(body);
|
||||||
const canvas = createCanvas(flipX ? data.width / 2 : data.width, flipY ? data.height / 2 : data.height);
|
magik.out('-liquid-rescale');
|
||||||
const ctx = canvas.getContext('2d');
|
magik.out(command);
|
||||||
ctx.drawImage(data, 0, 0, canvas.width, canvas.height);
|
magik.setFormat('png');
|
||||||
const attachment = canvas.toBuffer();
|
const attachment = await this.toBuffer(magik);
|
||||||
if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.');
|
if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.');
|
||||||
return msg.say({ files: [{ attachment, name: 'squish.png' }] });
|
return msg.say({ files: [{ attachment, name: 'squish.png' }] });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
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
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiao",
|
"name": "xiao",
|
||||||
"version": "117.8.0",
|
"version": "117.8.1",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user