I have a few <image>
elements in my SVG file:
<image id="image1" width="36" height="36" href="https://IMAGE_URL" preserveAspectRatio="xMidYMid slice" />
My final goal is to convert my SVG to PNG or JPEG but I can't do that if I have remote urls in my image elements. I tried multiple libraries (like npm sharp) and the images elements in the PNG result were empty.
One solution I thought about was to resolve all the image urls (href) in my SVG so it contains all the images data. For example:
<image href="data:image/jpeg;base64,ALL_IMAGE_DATA" />
But I couldn't find a good way to do that in node js.
Any ideas of how to do that? Or any other solutions to convert SVG to other image format while resolving all svg image elements?
CodePudding user response:
Try using this module
CodePudding user response:
The following asynchronous function takes an image URL and resolves to its data-URL:
function href2base64(url) {
return new Promise(function(resolve, reject) {
https.get(url).on("response", function(r) {
var buffers = [];
r.on("data", function(data) {
buffers.push(data);
}).on("end", function() {
resolve(`data:${r.headers["content-type"]};base64,${Buffer.concat(buffers).toString("base64")}`);
});
});
});
}