Skip to content

Commit

Permalink
Impact command
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabwhy committed Mar 3, 2021
1 parent 8dce377 commit da7eb2f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
5 changes: 5 additions & 0 deletions commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ motivate:
path: commands/image/motivate.js
category: Image
description: "Adds a demotivational poster caption to an image (usage: `motivate <text> | [text]`)"
impact:
type: script
path: commands/image/impact.js
category: Image
description: "Adds top and bottom text in the impact font (usage: `motivate <text> | [text]`)"
evenframes:
type: script
path: commands/image/evenframes.js
Expand Down
62 changes: 62 additions & 0 deletions commands/image/impact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require("dotenv").config()

let { findImage, sendImage } = require("../../modules/utils.js")

let { jimpReadURL, execGM, gmToBuffer } = require("@frogebot/image")({ imageMagick: process.env.USE_IMAGEMAGICK, maxGifSize: process.env.MAX_GIF_SIZE, maxImageSize: process.env.MAX_IMAGE_SIZE })
let { canvasText, canvasWindow } = require("../../modules/canvas.js");

var gm = require('gm');
if(process.env.USE_IMAGEMAGICK == "true") {
gm = gm.subClass({ imageMagick: true });
}

let procMsg
let imageUrl
async function cmdFunc(msg, args, startTime) {
try {
// Send processing message
procMsg = await msg.channel.send(process.env.MSG_PROCESSING);
msg.channel.startTyping()

imageUrl = await findImage(msg) // Find image in channel
let extension = imageUrl.split(".")[imageUrl.split(".").length-1].split("?")[0];

let imgFG = await jimpReadURL(imageUrl); // imageUrl to Jimp image

let height = Math.round(imgFG.bitmap.height); // Calculate new height
let width = Math.round(imgFG.bitmap.width); // Calculate new width

let textCanvas = await canvasText(args.split("|")[0].trim(), Math.round(width*0.1), "Impact", Math.round(width*0.9), "center", 1.5, "white", "transparent", "black", Math.round(width*0.0125)) // Create top text
let textCanvas2 = await canvasText(args.split("|").slice(1).join("|").trim(), Math.round(width*0.1), "Impact", Math.round(width*0.9), "center", 1.5, "white", "transparent", "black", Math.round(width*0.0125)) // Create bottom text

textCanvas[0] = await gmToBuffer(gm(textCanvas[0]).crop(width, Math.min(textCanvas[1], height))) // Crop top text
textCanvas2[0] = await gmToBuffer(gm(textCanvas2[0]).crop(width, textCanvas2[1])) // Crop bottom text

let img = await execGM(imageUrl, [
["composite", [textCanvas[0], Math.round(width*0.05), Math.round(width*0.05 - imgFG.bitmap.width*0.025)]],
["composite", [textCanvas2[0], Math.round(width*0.05), Math.round(height - width*0.05 + imgFG.bitmap.width*0.025)]]
]) // Execute image manipulation

sendImage(msg, "Impact", startTime, img, extension, procMsg) // Send image
} catch(e) {
console.log(e)
msg.channel.stopTyping()
msg.channel.send({
embed: {
"title": "Error",
"description": `<@${msg.author.id}> - ${ imageUrl != undefined ? process.env.MSG_ERROR : process.env.MSG_NO_IMAGE}`,
"color": Number(process.env.EMBED_COLOUR),
"timestamp": new Date(),
"author": {
"name": process.env.BOT_NAME,
"icon_url": msg.client.user.displayAvatarURL()
}
}
})
procMsg.delete();
}
}

module.exports = {
cmdFunc
}
Binary file added fonts/Impact.ttf
Binary file not shown.
30 changes: 28 additions & 2 deletions modules/canvas.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { createCanvas, loadImage, registerFont } = require("canvas");
const { fillTextWithTwemoji, measureText } = require("./emojiCanvasText");
const { fillTextWithTwemoji, strokeTextWithTwemoji, measureText } = require("./emojiCanvasText");

// Fonts
registerFont(__dirname + "/../fonts/RobotoBlack.ttf", { family: "Roboto" });
registerFont(__dirname + "/../fonts/Matoran.ttf", { family: "Matoran" });
registerFont(__dirname + "/../fonts/Impact.ttf", { family: "Impact Condensed" });

function canvasText(
text,
Expand All @@ -13,7 +14,9 @@ function canvasText(
align = "center",
lineSpacing = 1.5,
fillStyle = "black",
bg = "transparent"
bg = "transparent",
strokeStyle = "transparent",
lineWidth = 0
) {
return new Promise(async (resolve, reject) => {
// Init canvas
Expand All @@ -28,6 +31,10 @@ function canvasText(
}

// Text styling
ctx.strokeStyle = strokeStyle
ctx.lineWidth = lineWidth;
ctx.lineJoin="miter";
ctx.miterLimit=2;
ctx.fillStyle = fillStyle;
ctx.textAlign = align;
ctx.font = fontSize + "px " + fontFamily;
Expand Down Expand Up @@ -60,6 +67,9 @@ function printAtWordWrap(context, text, x, y, lineHeight, fitWidth) {
let forcedLines = text.split("\n"); // Create new lines where they exist in the input text

if (fitWidth <= 0 && forcedLines.length <= 1) {
await strokeTextWithTwemoji(context, text, x, y, {
emojiTopMarginPercent: 0.1,
}); // Strokes text using Twemoji support
await fillTextWithTwemoji(context, text, x, y, {
emojiTopMarginPercent: 0.1,
}); // Fills text using Twemoji support
Expand All @@ -77,6 +87,14 @@ function printAtWordWrap(context, text, x, y, lineHeight, fitWidth) {
if (idx == 1) {
idx = 2;
}
await strokeTextWithTwemoji(
context,
words.slice(0, idx - 1).join(" "),
lineHeight,
x,
y + lineHeight * currentLine,
{ emojiTopMarginPercent: 0.15 }
); // Strokes text using Twemoji support
await fillTextWithTwemoji(
context,
words.slice(0, idx - 1).join(" "),
Expand All @@ -93,6 +111,14 @@ function printAtWordWrap(context, text, x, y, lineHeight, fitWidth) {
}
}
if (idx > 0)
await strokeTextWithTwemoji(
context,
words.join(" "),
lineHeight,
x,
y + lineHeight * currentLine,
{ emojiTopMarginPercent: 0.15 }
); // Strokes text using Twemoji support
await fillTextWithTwemoji(
context,
words.join(" "),
Expand Down

0 comments on commit da7eb2f

Please sign in to comment.