My current challenge involves converting an image to base 64 format using its URL.
This is the method I am currently using:
convertToBase64(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
// Attempting to convert an image from its URL to base 64 format using the function above
this.convertToBase64('https://live.mystocks.co.ke/research/images/bamburi.jpg');
Unfortunately, this approach fails to produce the desired results.
I need help in identifying what might be going wrong and how I can successfully achieve the conversion.