Use resvg instead of sharp

Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
This commit is contained in:
Joshua Castle
2024-03-24 00:35:39 -07:00
parent 9167ba6b07
commit 46a868f1f8
5 changed files with 93 additions and 265 deletions
-1
View File
@@ -9,7 +9,6 @@ export type ImageGeneratorResult = {
export declare class ImageGenerator {
private args;
private satori;
private sharp;
private cache;
private outDir;
constructor(args: {
+3 -3
View File
@@ -6,10 +6,10 @@ const fs = tslib_1.__importStar(require("fs"));
const fsp = tslib_1.__importStar(require("fs/promises"));
const object_hash_1 = tslib_1.__importDefault(require("object-hash"));
const path_1 = tslib_1.__importDefault(require("path"));
const resvg_js_1 = require("@resvg/resvg-js");
class ImageGenerator {
args;
satori;
sharp;
cache = {};
outDir = '';
constructor(args) {
@@ -18,7 +18,6 @@ class ImageGenerator {
}
init = async () => {
this.satori = await import('satori').then((mod) => mod.default);
this.sharp = await import('sharp').then((mod) => mod.default);
if (!fs.existsSync(this.outDir))
await fsp.mkdir(this.outDir, { recursive: true });
};
@@ -30,7 +29,8 @@ class ImageGenerator {
const absolutePath = path_1.default.join(this.outDir, imageName);
const relativePath = path_1.default.join('/', this.args.dir, absolutePath.slice(this.outDir.length));
const svg = await this.satori(element, options);
await this.sharp(Buffer.from(svg)).png().toFile(absolutePath);
const pngBuffer = new resvg_js_1.Resvg(svg).render().asPng();
fs.writeFileSync(absolutePath, pngBuffer);
const url = new URL(this.args.websiteUrl);
url.pathname = relativePath;
this.cache[hash] = {