Skip to content

Commit

Permalink
Finally fix broken svg converting by switching to sharp
Browse files Browse the repository at this point in the history
  • Loading branch information
coopw1 committed Dec 22, 2024
1 parent b955712 commit c8d0527
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/commands/util/convertSvgToPng.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const DOMParser = new JSDOM().window.DOMParser;
const canvas = require("canvas");
const fetch = require("node-fetch");
const { Canvg, presets } = require("canvg");
const axios = require("axios");
const sharp = require("sharp");

const { devEmail } = require("../../../config.json");

const preset = presets.node({
DOMParser,
canvas,
fetch,
});

/**
* Retrieves an SVG image from the specified link and converts it to a PNG image.
*
Expand All @@ -25,13 +15,14 @@ module.exports = async (link) => {
"User-Agent": `DiscordBrainzBot/1.0.0 (${devEmail})`,
});
const svg = response.data;
const canvas = preset.createCanvas(800, 600);
const ctx = canvas.getContext("2d");
const v = Canvg.fromString(ctx, svg, preset);

// Render only first frame, ignoring animations.
await v.render();
const image = await sharp(Buffer.from(svg))
.png()
.toBuffer()
.catch((error) => {
console.error(error);
return null;
});

const png = canvas.toBuffer();
return png;
return image;
};

0 comments on commit c8d0527

Please sign in to comment.