3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

Use photon-node instead of sharp for image manipulation

photon-node is compiled to wasm so doesn't require a node-gyp build step
This commit is contained in:
Dragory 2021-08-14 15:45:47 +03:00
parent 088b9a16b1
commit ba7ece8df7
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
3 changed files with 91 additions and 877 deletions

File diff suppressed because it is too large Load diff

View file

@ -23,6 +23,7 @@
"test-watch": "tsc-watch --onSuccess \"npx ava\""
},
"dependencies": {
"@silvia-odwyer/photon-node": "^0.3.1",
"bufferutil": "^4.0.3",
"cors": "^2.8.5",
"cross-env": "^5.2.0",
@ -58,7 +59,6 @@
"regexp-worker": "^1.1.0",
"safe-regex": "^2.0.2",
"seedrandom": "^3.0.1",
"sharp": "github:almeidx/sharp#68b4f387ae2ee1ee2dd8f289f5ec5fcf722fd3d3",
"strip-combining-marks": "^1.0.0",
"tlds": "^1.203.1",
"tmp": "0.0.33",
@ -82,7 +82,6 @@
"@types/passport-oauth2": "^1.4.8",
"@types/passport-strategy": "^0.2.35",
"@types/safe-regex": "^1.1.2",
"@types/sharp": "^0.23.1",
"@types/tmp": "0.0.33",
"@types/twemoji": "^12.1.0",
"ava": "^3.10.0",

View file

@ -1,6 +1,6 @@
import { MessageAttachment } from "discord.js";
import fs from "fs";
import sharp from "sharp";
import photon from "@silvia-odwyer/photon-node";
import twemoji from "twemoji";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage } from "../../../pluginUtils";
@ -14,12 +14,24 @@ async function getBufferFromUrl(url: string): Promise<Buffer> {
return fsp.readFile(downloadedEmoji.path);
}
async function resizeBuffer(input: Buffer, width: number, height: number): Promise<Buffer> {
return sharp(input, { density: 800 })
.resize(width, height, {
fit: "inside",
})
.toBuffer();
function bufferToPhotonImage(input: Buffer): photon.PhotonImage {
const base64 = input
.toString("base64")
.replace(/^data:image\/\w+;base64,/, "");
return photon.PhotonImage.new_from_base64(base64);
}
function photonImageToBuffer(image: photon.PhotonImage): Buffer {
const base64 = image.get_base64()
.replace(/^data:image\/\w+;base64,/, "");
return Buffer.from(base64, "base64");
}
function resizeBuffer(input: Buffer, width: number, height: number): Buffer {
const photonImage = bufferToPhotonImage(input);
photon.resize(photonImage, width, height, photon.SamplingFilter.Lanczos3);
return photonImageToBuffer(photonImage);
}
const CDN_URL = "https://twemoji.maxcdn.com/2/svg";