I have the following
HTML
<img id="preview1" crossorigin="anonymous" src="http://www.gravatar.com/avatar/0e39d18b89822d1d9871e0d1bc839d06?s=128&d=identicon&r=PG">
JS
var img = document.getElementById("preview1");
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.width;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
var result = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
console.log(result)
For some reason, when I paste the result
(base64) in an img
src
it shows nothing. Also when I paste the result
(base64) as a url, it shows nothing.
When I use use input="file"
and use FileReader()
to get the file. it works fine.
Is it a better way to grab a remote file and convert to a working base64?