admin管理员组文章数量:1200396
I have two Set of Code for testing html5 canvas
Set 1 - Work perfectly
<img id="preview" src=";d=identicon&r=PG">
<canvas id="myCanvas"/>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("preview");
ctx.drawImage(img, 10, 10);
alert(c.toDataURL());
Set 2 - Show exception error (Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': tainted canvases may not be exported. )
<img id="preview1" src=";d=identicon&r=PG">
function getBase64() {
var img = new Image();
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"); //This line of code will throw exception
alert( dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
}
I have no idea why in Set 1 toDataURL
is not throwing any exception where Set toDataURL
will throw exception and both are using same set of image.The different is in the first set i hardcode the canvas in HTML , and second set i create it through javascript.
My objective for Set 2 code is to get 64 base encode of the image.
I have two Set of Code for testing html5 canvas
Set 1 - Work perfectly
<img id="preview" src="http://www.gravatar.com/avatar/0e39d18b89822d1d9871e0d1bc839d06?s=128&d=identicon&r=PG">
<canvas id="myCanvas"/>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("preview");
ctx.drawImage(img, 10, 10);
alert(c.toDataURL());
Set 2 - Show exception error (Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': tainted canvases may not be exported. )
<img id="preview1" src="http://www.gravatar.com/avatar/0e39d18b89822d1d9871e0d1bc839d06?s=128&d=identicon&r=PG">
function getBase64() {
var img = new Image();
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"); //This line of code will throw exception
alert( dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
}
I have no idea why in Set 1 toDataURL
is not throwing any exception where Set toDataURL
will throw exception and both are using same set of image.The different is in the first set i hardcode the canvas in HTML , and second set i create it through javascript.
My objective for Set 2 code is to get 64 base encode of the image.
Share Improve this question asked Nov 17, 2013 at 6:15 abc cbaabc cba 2,63311 gold badges32 silver badges50 bronze badges 1- ` canvas.width =img.width; canvas.height =img.width;` ? – RegarBoy Commented Feb 9, 2016 at 17:59
2 Answers
Reset to default 13You can just use crossOrigin Attribute.
var img= new Image();
img.setAttribute('crossOrigin', 'anonymous');
img.src = url;
This is a CORS issue - Gravatar sends the correct headers, you just need to put the correct attribute in:
<img id="preview1" crossorigin="anonymous" src="http://www.gravatar.com/avatar/0e39d18b89822d1d9871e0d1bc839d06?s=128&d=identicon&r=PG">
function getBase64() {
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");
alert(dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
}
getBase64();
Note that your first example also failed when I tested it, just as it should.
本文标签: javascripttoDataURL throw Uncaught Security exceptionStack Overflow
版权声明:本文标题:javascript - toDataURL throw Uncaught Security exception - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738604713a2102277.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论