Add uncanny, detect filename from image link

This commit is contained in:
Essem 2022-07-30 16:00:09 -05:00
parent e9a9680a7d
commit f3eea0d96a
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
18 changed files with 183 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 355 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

View file

@ -72,6 +72,7 @@ class ImageCommand extends Command {
magickParams.path = image.path; magickParams.path = image.path;
magickParams.params.type = image.type; magickParams.params.type = image.type;
magickParams.url = image.url; // technically not required but can be useful for text filtering magickParams.url = image.url; // technically not required but can be useful for text filtering
magickParams.name = image.name;
if (this.constructor.requiresGIF) magickParams.onlyGIF = true; if (this.constructor.requiresGIF) magickParams.onlyGIF = true;
} catch (e) { } catch (e) {
runningCommands.delete(this.author.id); runningCommands.delete(this.author.id);
@ -89,7 +90,7 @@ class ImageCommand extends Command {
switch (typeof this.params) { switch (typeof this.params) {
case "function": case "function":
Object.assign(magickParams.params, this.params(magickParams.url)); Object.assign(magickParams.params, this.params(magickParams.url, magickParams.name));
break; break;
case "object": case "object":
Object.assign(magickParams.params, this.params); Object.assign(magickParams.params, this.params);

View file

@ -0,0 +1,61 @@
import ImageCommand from "../../classes/imageCommand.js";
import { random } from "../../utils/misc.js";
import { readdirSync } from "fs";
import { resolve, dirname } from "path";
import { fileURLToPath } from "url";
const prompts = ["you found:", "your dad is:", "you ate:", "your mom is:", "your sister is:", "you saw:", "you get lost in:", "you find:", "you grab:", "you pull out of your pocket:", "you fight:", "it's in your room:"];
const names = readdirSync(resolve(dirname(fileURLToPath(import.meta.url)), "../../assets/images/uncanny/")).map((val) => {
return val.split(".")[0];
});
class UncannyCommand extends ImageCommand {
params(url, name = "unknown") {
const newArgs = this.options.text ?? this.args.filter(item => !item.includes(url)).join(" ");
// eslint-disable-next-line prefer-const
let [text1, text2] = newArgs.split(/(?<!\\),/).map(elem => elem.trim());
if (!text2?.trim()) text2 = name;
return {
caption: text1?.trim() ? text1.replaceAll("&", "&amp;").replaceAll(">", "&gt;").replaceAll("<", "&lt;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;").replaceAll("\\n", "\n") : random(prompts),
caption2: text2.replaceAll("&", "&amp;").replaceAll(">", "&gt;").replaceAll("<", "&lt;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;").replaceAll("\\n", "\n"),
path: `./assets/images/uncanny/${typeof this.options.phase === "string" && names.includes(this.options.phase.toLowerCase()) ? this.options.phase.toLowerCase() : random(names)}.png`,
font: typeof this.options.font === "string" && this.constructor.allowedFonts.includes(this.options.font.toLowerCase()) ? this.options.font.toLowerCase() : "helvetica"
};
}
static init() {
super.init();
this.flags.push({
name: "font",
type: 3,
choices: (() => {
const array = [];
for (const font of this.allowedFonts) {
array.push({ name: font, value: font });
}
return array;
})(),
description: "Specify the font you want to use (default: helvetica)"
}, {
name: "phase",
type: 3,
choices: (() => {
const array = [];
for (const name of names) {
array.push({ name, value: name });
}
return array;
})(),
description: "Specify the uncanny image you want to use"
});
return this;
}
static description = "Makes a Mr. Incredible Becomes Uncanny image (separate left/right text with a comma)";
static aliases = ["canny", "incredible", "pain"];
static arguments = ["{left text}", "{right text}"];
static noImage = "You need to provide an image/GIF to create an uncanny image!";
static command = "uncanny";
}
export default UncannyCommand;

View file

@ -30,6 +30,7 @@
#include "spin.h" #include "spin.h"
#include "tile.h" #include "tile.h"
#include "togif.h" #include "togif.h"
#include "uncanny.h"
#include "uncaption.h" #include "uncaption.h"
#include "wall.h" #include "wall.h"
#include "watermark.h" #include "watermark.h"
@ -79,6 +80,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
exports.Set(Napi::String::New(env, "swirl"), Napi::Function::New(env, Swirl)); exports.Set(Napi::String::New(env, "swirl"), Napi::Function::New(env, Swirl));
exports.Set(Napi::String::New(env, "tile"), Napi::Function::New(env, Tile)); exports.Set(Napi::String::New(env, "tile"), Napi::Function::New(env, Tile));
exports.Set(Napi::String::New(env, "togif"), Napi::Function::New(env, ToGif)); exports.Set(Napi::String::New(env, "togif"), Napi::Function::New(env, ToGif));
exports.Set(Napi::String::New(env, "uncanny"), Napi::Function::New(env, Uncanny));
exports.Set(Napi::String::New(env, "uncaption"), Napi::Function::New(env, Uncaption)); exports.Set(Napi::String::New(env, "uncaption"), Napi::Function::New(env, Uncaption));
exports.Set(Napi::String::New(env, "wall"), Napi::Function::New(env, Wall)); exports.Set(Napi::String::New(env, "wall"), Napi::Function::New(env, Wall));
exports.Set(Napi::String::New(env, "watermark"), Napi::Function::New(env, Watermark)); exports.Set(Napi::String::New(env, "watermark"), Napi::Function::New(env, Watermark));

109
natives/uncanny.cc Normal file
View file

@ -0,0 +1,109 @@
#include <iostream>
#include <napi.h>
#include <vips/image.h>
#include <vips/vips8>
using namespace std;
using namespace vips;
Napi::Value Uncanny(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Object result = Napi::Object::New(env);
try {
Napi::Object obj = info[0].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string caption = obj.Get("caption").As<Napi::String>().Utf8Value();
string caption2 = obj.Get("caption2").As<Napi::String>().Utf8Value();
string font = obj.Get("font").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
VOption *options = VImage::option()->set("access", "sequential");
VImage in =
VImage::new_from_buffer(data.Data(), data.Length(), "",
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB)
.extract_band(0, VImage::option()->set("n", 3));
VImage base = VImage::black(1280, 720, VImage::option()->set("bands", 3));
string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " +
(font != "impact" ? "bold" : "normal") + " 72";
string captionText = "<span background=\"black\" foreground=\"white\">" +
caption + "</span>";
string caption2Text =
"<span background=\"black\" foreground=\"red\">" + caption2 + "</span>";
VImage text =
VImage::text(captionText.c_str(), VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", font_string.c_str())
->set("width", 588)
->set("height", 90));
VImage captionImage =
text.extract_band(0, VImage::option()->set("n", 3))
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, 640, text.height() + 40,
VImage::option()->set("extend", "black"));
VImage text2 = VImage::text(caption2Text.c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", font_string.c_str())
->set("width", 588)
->set("height", 90));
VImage caption2Image =
text2.extract_band(0, VImage::option()->set("n", 3))
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, 640, text.height() + 40,
VImage::option()->set("extend", "black"));
base = base.insert(captionImage, 0, 0).insert(caption2Image, 640, 0);
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = vips_image_get_n_pages(in.get_image());
VImage uncanny = VImage::new_from_file(path.c_str());
base = base.insert(uncanny, 0, 130);
vector<VImage> img;
for (int i = 0; i < nPages; i++) {
VImage img_frame =
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
VImage resized = img_frame.resize(690.0 / (double)width);
if (resized.height() > 590) {
double vscale = 590.0 / (double)resized.height();
resized =
resized.resize(vscale, VImage::option()->set("vscale", vscale));
}
VImage composited = base.insert(resized, 935 - (resized.width() / 2),
425 - (resized.height() / 2));
img.push_back(composited);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, 720);
void *buf;
size_t length;
final.write_to_buffer(("." + type).c_str(), &buf, &length,
type == "gif" ? VImage::option()->set("reoptimise", 1)
: 0);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear();
vips_thread_shutdown();
return result;
}

5
natives/uncanny.h Normal file
View file

@ -0,0 +1,5 @@
#pragma once
#include <napi.h>
Napi::Value Uncanny(const Napi::CallbackInfo& info);

View file

@ -38,9 +38,12 @@ const videoFormats = ["video/mp4", "video/webm", "video/mov"];
// gets the proper image paths // gets the proper image paths
const getImage = async (image, image2, video, extraReturnTypes, gifv = false, type = null, link = false) => { const getImage = async (image, image2, video, extraReturnTypes, gifv = false, type = null, link = false) => {
try { try {
const fileNameSplit = new URL(image).pathname.split("/");
const fileName = fileNameSplit[fileNameSplit.length - 1].split(".")[0];
const payload = { const payload = {
url: image2, url: image2,
path: image path: image,
name: fileName
}; };
const host = new URL(image2).host; const host = new URL(image2).host;
if (gifv || (link && combined.includes(host))) { if (gifv || (link && combined.includes(host))) {