I would like to use svg
image that I have from url
, convert it to base64
. Later on I am posting this image on social media platform. That is why I need it to be in base64
format. How can I achieve that? I know how to do that when I have this picture on my website but how can I do that when I have only a link to that image?
Here is the simple code which can be used in node.js using node-fetch library.
import fetch from 'node-fetch';
const download = async () => {
const response = await fetch("https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/410.svg");
if (response.ok) {
const buffer = await response.buffer();
const base64 = buffer.toString("base64");
return base64;
}
}
(async () => {
const data = await download();
console.log("🚀 ~ file: base64.js ~ line 12 ~ data", data);
})();
I'm using sample svg image from this link . Above code will return you base64 of the sample svg image:
PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDAgMTAwIj4KICA8cGF0aCBkPSJNMzAsMWg0MGwyOSwyOXY0MGwtMjksMjloLTQwbC0yOS0yOXYtNDB6IiBzdHJva2U9IiMwMDAiIGZpbGw9Im5vbmUiLz4gCiAgPHBhdGggZD0iTTMxLDNoMzhsMjgsMjh2MzhsLTI4LDI4aC0zOGwtMjgtMjh2LTM4eiIgZmlsbD0iI2EyMyIvPiAKICA8dGV4dCB4PSI1MCIgeT0iNjgiIGZvbnQtc2l6ZT0iNDgiIGZpbGw9IiNGRkYiIHRleHQtYW5jaG9yPSJtaWRkbGUiPjwhW0NEQVRBWzQxMF1dPjwvdGV4dD4KPC9zdmc+Cg==
If you copy and paste this here , you will see the preview of the same image. Voila !!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.