Skip to content

Instantly share code, notes, and snippets.

@fabiokandrade
Created November 2, 2016 12:38
Show Gist options
  • Save fabiokandrade/d2cfab70d4f5d599f4adb4a80070b124 to your computer and use it in GitHub Desktop.
Save fabiokandrade/d2cfab70d4f5d599f4adb4a80070b124 to your computer and use it in GitHub Desktop.
function getBase64FromImageUrl(url) {
var img = new Image();
img.setAttribute('crossOrigin', 'anonymous');
img.onload = function () {
var canvas = document.createElement("canvas");
canvas.width =this.width;
canvas.height =this.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0);
var dataURL = canvas.toDataURL("image/png");
alert(dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
};
img.src = url;
}
@prakash2091
Copy link

i still face the issue of CROS when i try with above code.

"https://abcd.com" from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment