From bc009c4fbf68117678a7e7186594520f3fa755bf Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 29 Apr 2024 01:17:07 -0400 Subject: [PATCH] Crop to content command --- README.md | 3 +- commands/edit-image/crop-to-content.js | 42 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 commands/edit-image/crop-to-content.js diff --git a/README.md b/README.md index cbce97e0..86573102 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Just read `LICENSE.md`. Give credit if you use any part of this monster, thanks. 22. Start Xiao up! ## Commands -Total: 510 +Total: 511 ### Utility: @@ -392,6 +392,7 @@ Total: 510 * **contrast:** Draws an image or a user's avatar but with contrast. * **convert-image:** Converts an image from one format to another. * **create-qr-code:** Converts text to a QR Code. +* **crop-to-content:** Crops an image to its content. * **desaturate:** Draws an image or a user's avatar but desaturated. * **dexter:** Draws an image or a user's avatar over the screen of Dexter from Pokémon. * **distort:** Draws an image or a user's avatar but distorted. diff --git a/commands/edit-image/crop-to-content.js b/commands/edit-image/crop-to-content.js new file mode 100644 index 00000000..71254450 --- /dev/null +++ b/commands/edit-image/crop-to-content.js @@ -0,0 +1,42 @@ +const Command = require('../../framework/Command'); +const { PermissionFlagsBits } = require('discord.js'); +const { createCanvas, loadImage } = require('canvas'); +const request = require('node-superfetch'); +const { cropToContent } = require('../../util/Canvas'); + +module.exports = class CropToContentCommand extends Command { + constructor(client) { + super(client, { + name: 'crop-to-content', + group: 'edit-image', + memberName: 'crop-to-content', + description: 'Crops an image to its content.', + throttling: { + usages: 2, + duration: 10 + }, + clientPermissions: [PermissionFlagsBits.AttachFiles], + args: [ + { + key: 'image', + type: 'image' + } + ] + }); + } + + async run(msg, { image }) { + const { body } = await request.get(image); + const data = await loadImage(body); + const canvas = createCanvas(data.width, data.height); + const ctx = canvas.getContext('2d'); + ctx.drawImage(data, 0, 0); + cropToContent(ctx, canvas, data.width, data.height); + if (canvas.width === data.width && canvas.height === data.height) { + return msg.say('Looks like this image is already cropped to its content.'); + } + const attachment = canvas.toBuffer(); + if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); + return msg.say({ files: [{ attachment, name: 'crop-to-content.png' }] }); + } +};