From b424b0f69d4d33fdaca1fc5732397ca1bf878a84 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Sun, 13 Oct 2019 16:54:07 +0200 Subject: [PATCH] various image manipulation command --- commands/images/autocrop.js | 53 ++++++++++++++++++++++++++++++++++ commands/images/jpegify.js | 51 +++++++++++++++++++++++++++++++++ commands/images/mirror.js | 52 +++++++++++++++++++++++++++++++++ commands/images/rotate.js | 57 +++++++++++++++++++++++++++++++++++++ 4 files changed, 213 insertions(+) create mode 100644 commands/images/autocrop.js create mode 100644 commands/images/jpegify.js create mode 100644 commands/images/mirror.js create mode 100644 commands/images/rotate.js diff --git a/commands/images/autocrop.js b/commands/images/autocrop.js new file mode 100644 index 00000000..f8285dc0 --- /dev/null +++ b/commands/images/autocrop.js @@ -0,0 +1,53 @@ +const { Command } = require('discord-akairo'); +const jimp = require('jimp'); +const os = require('os'); + +class autocropCommand extends Command { + constructor() { + super('autocrop', { + aliases: ['autocrop', 'crop'], + category: 'images', + args: [ + { + id: 'link', + type: 'string', + } + ], + description: { + content: 'autocrop image border of the same color', + usage: '[link to image]', + examples: [''] + } + }); + } + + async exec(message, args) { + let output = `${os.tmpdir()}/cropped${message.id}.jpg`; + + + let Attachment = (message.attachments).array(); + let url = args.link; + // Get attachment link + if (Attachment[0] && !args.link) { + url = Attachment[0].url; + } + + + jimp.read({ + url: url + }) + .then(image => { + return image + .autocrop() + .write(output); + }) + .then(() => { + return message.channel.send({files: [output]}); + }); + + + + } +} + +module.exports = autocropCommand; \ No newline at end of file diff --git a/commands/images/jpegify.js b/commands/images/jpegify.js new file mode 100644 index 00000000..f5764a85 --- /dev/null +++ b/commands/images/jpegify.js @@ -0,0 +1,51 @@ +const { Command } = require('discord-akairo'); +const jimp = require('jimp'); +const os = require('os'); + +class jpegifyCommand extends Command { + constructor() { + super('jpegify', { + aliases: ['jpegify'], + category: 'images', + args: [ + { + id: 'link', + type: 'string', + } + ], + description: { + content: 'jpegify your image', + usage: '[link to image]', + examples: [''] + } + }); + } + + async exec(message, args) { + let output = `${os.tmpdir()}/jpegified${message.id}.jpg`; + + + let Attachment = (message.attachments).array(); + let url = args.link; + // Get attachment link + if (Attachment[0] && !args.link) { + url = Attachment[0].url; + } + + jimp.read({ + url: url + }) + .then(image => { + return image + .quality(1) + .write(output); + }) + .then(() => { + return message.channel.send({files: [output]}); + }); + + + } +} + +module.exports = jpegifyCommand; \ No newline at end of file diff --git a/commands/images/mirror.js b/commands/images/mirror.js new file mode 100644 index 00000000..8e1541eb --- /dev/null +++ b/commands/images/mirror.js @@ -0,0 +1,52 @@ +const { Command } = require('discord-akairo'); +const jimp = require('jimp'); +const os = require('os'); + +class mirrorCommand extends Command { + constructor() { + super('mirror', { + aliases: ['mirror', 'flip'], + category: 'images', + args: [ + { + id: 'link', + type: 'string', + } + ], + description: { + content: 'mirror the image.', + usage: '[link to image]', + examples: [''] + } + }); + } + + async exec(message, args) { + let output = `${os.tmpdir()}/mirrored${message.id}.jpg`; + + + let Attachment = (message.attachments).array(); + let url = args.link; + // Get attachment link + if (Attachment[0] && !args.link) { + url = Attachment[0].url; + } + + + jimp.read({ + url: url + }) + .then(image => { + return image + .mirror(true, false) + .write(output); + }) + .then(() => { + return message.channel.send({files: [output]}); + }); + + + } +} + +module.exports = mirrorCommand; \ No newline at end of file diff --git a/commands/images/rotate.js b/commands/images/rotate.js new file mode 100644 index 00000000..00b6fbe6 --- /dev/null +++ b/commands/images/rotate.js @@ -0,0 +1,57 @@ +const { Command } = require('discord-akairo'); +const jimp = require('jimp'); +const os = require('os'); + +class rotateCommand extends Command { + constructor() { + super('rotate', { + aliases: ['rotate'], + category: 'images', + args: [ + { + id: 'link', + type: 'string', + }, + { + id: 'rotate', + type: 'integer', + } + ], + description: { + content: 'Make your vid shit quality.', + usage: '[link to image] [angle of rotation]', + examples: [''] + } + }); + } + + async exec(message, args) { + let output = `${os.tmpdir()}/rotated${message.id}.jpg`; + + + let Attachment = (message.attachments).array(); + let url = args.link; + // Get attachment link + if (Attachment[0] && !args.link) { + url = Attachment[0].url; + } + + + jimp.read({ + url: url + }) + .then(image => { + return image + .rotate(args.rotate) + .write(output); + }) + .then(() => { + return message.channel.send({files: [output]}); + }); + + + + } +} + +module.exports = rotateCommand; \ No newline at end of file