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:
parent
088b9a16b1
commit
ba7ece8df7
3 changed files with 91 additions and 877 deletions
|
@ -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";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue