How to allow cross-origin access to imgs and canvases?

柔情痞子 提交于 2020-01-24 22:54:07

问题


I have problem with this code:

var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);

Error from Google Chrome console is:

Unable to get image data from canvas because the canvas has been tainted by cross-origin data.

It's because i have run this site on the domain1.com, img src is on the domain2.com and later i want to set new src with a local (modified) data (as below), i think.

return canvas.toDataURL();

I have tried many ways to allow cross-origin access, but nothing works:

if (canvas.crossOriginKeyword)
{
     canvas.removeAttribute("crossorigin");
}

if (canvasContext.crossOriginKeyword)
{
    canvasContext.removeAttribute("crossorigin");
}

My .htaccess file contains:

Header always set Access-Control-Allow-Origin *

And I also tried:

<?php
header('Access-Control-Allow-Origin: *');
?>

My Script should work on many browsers, and many servers, so i want to avoid modifying browser or apache configuration, unless it's really needed.

I can't use a proxy.

Please, help me :)

来源:https://stackoverflow.com/questions/11794025/how-to-allow-cross-origin-access-to-imgs-and-canvases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!