1
function getBase64Image(img) {
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    document.body.appendChild(canvas);
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    var dataURL = canvas.toDataURL("image/png");
    return dataURL;
}   

I seem to be violating something, just can't seem to find what. Any help on how to fix it?

Manak Kapoor
  • 962
  • 3
  • 12
  • 21

1 Answers1

3

If the image you drew to the canvas id from another domain then your canvas is tainted and you cannot produce a data uri ftom it. Copy the image over to the same domain as the script to prevent this.

Musa
  • 96,336
  • 17
  • 118
  • 137
  • But what if the code is running as a chrome extension, and doesn't really have a domain? – Manak Kapoor Apr 18 '13 at 02:22
  • @ManakKapoor take a look at this question http://stackoverflow.com/questions/5511378/security-err-dom-exception-18-on-using-getimagedata-in-a-chrome-extension – Musa Apr 18 '13 at 02:25