From 8a40b24cd6ca7758b378d288937f3c2b16de442e Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Tue, 30 Jun 2020 15:26:15 -0400 Subject: [PATCH] Make squish use imagemagick --- commands/edit-image/squish.js | 29 ++++++++++++++++++++--------- package.json | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/commands/edit-image/squish.js b/commands/edit-image/squish.js index 438b0cfe..b6c28324 100644 --- a/commands/edit-image/squish.js +++ b/commands/edit-image/squish.js @@ -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); + }); + }); + } +}; }; diff --git a/package.json b/package.json index 3593e6a2..c230d466 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "117.8.0", + "version": "117.8.1", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {