简体   繁体   English

使用fabric.fromUrl时出现CORS问题

[英]CORS issue when using fabric.fromUrl

I am integrating fabricjs with angularjs application. 我正在将fabric.js与angularjs应用程序集成在一起。 I am pulling an image from a third party source (which is not in my control). 我正在从第三方来源(我无法控制)中提取图像。 I wish to perform some actions on it like: filtering, adding to canvas, storing to canvas and reloading from canvas. 我希望对其执行一些操作,例如:过滤,添加到画布,存储到画布以及从画布重新加载。

I am using fabric fromurl call with crossorigin but it fails everytime. 我正在使用带有交叉源的fabric fromurl调用,但是每次都失败。

 fabric.Image.fromURL('http://img.fkcdn.com/image/dining-chair/b/g/y/fidcbennywncsach-1-cedar-pine-devdar-home-cherry-white-original-imae9fsfbkxrgebt.jpeg', function (img) {
    canvas1.add(img.set({
        left: 50,
        top: 50,
        angle: 30
    }));
    console.log('CORS enabled + crossOrigin property - DataURL: ', canvas1.toDataURL());
}, {
    crossOrigin: 'Anonymous'
});

Fiddle 小提琴

Is there anything I am doing wrong? 我做错了什么吗?

crossOrigin will only request permission to use the resource over CORS but the server can deny it, which in case will also make loading the image fail all together. crossOrigin将仅请求通过CORS使用资源的权限,但服务器可以拒绝它,以防万一还会使图像加载全部失败。

The only way around is to upload the image to your own server (no crossOrigin needed) or to use a CORS proxy ( crossOrigin still needed) or to upload the image to a host that allows CORS usage (imgur.com and dropbox.com being a couple of examples). 唯一的解决方法是将映像上载到您自己的服务器(不需要crossOrigin )或使用CORS代理(仍然需要crossOrigin )或将映像上载到允许使用CORS的主机(imgur.com和dropbox.com几个例子)。 All these workaround may involve user right issues. 所有这些变通办法可能涉及用户权限问题。

Fabricjs Code Snippet Fabricjs代码段

 fabric.util.loadImage(img.src, function(any image url on website) {
    var object = new fabric.Image(any image url on website);
    object.set({
      id: 'bg',
      width: 100,
      height: 100,
      left: 20,
      top: 30
    });
    canvas.add(object);

  }, null, {
    crossOrigin: 'anonymous'
  })

To Avoid Cors Issue in fabricjs 避免fabricjs中的Cors问题

1 : Use fabric.util.loadImage Instead of fabric.Image.fromURL 1:使用fabric.util.loadImage而不是fabric.Image.fromURL

2 : fabric.util.loadImage Takes 3 arguments and 3rd arg should be crossOrigin: 'anonymous' like in below code 2: fabric.util.loadImage 3个参数,第三个arg应该是crossOrigin: 'anonymous'像下面的代码

3 : If you are showing image in html use crossOrigin = 'anonymous' in image tag in html 3:如果要在html中显示图像,请在html的图像标签中使用crossOrigin = 'anonymous'

4 : there will be cors error if you website is unsecure and you want to pull image from secure website 4:如果您的网站不安全并且想要从安全网站中提取图片则会出现cors错误

eg : https Vs http 例如: https vs http

  • you are on localhost:7000 (unsecure) and want to pull image from https://hello.com/image2.png (secure) 您在localhost:7000 (不安全),并且想从https://hello.com/image2.png提取图像(安全)

  • you are on http://www.hiee.com and want to pull image from https://hello.com/image2.png (secure) 您在http://www.hiee.com并希望从https://hello.com/image2.png提取图像(安全)

5 : if you still have cors issue then the server from which you are pulling image should also send these headers with image "Access-Control-Allow-Origin", "*" .Well how to checkout these headers , is open image in browser tab then firebug->network , see headers 5:如果仍然出现cors问题,则要从中拉出图像的服务器也应发送带有图像"Access-Control-Allow-Origin", "*"标头。如何检出这些标头,请在浏览器中打开图像标签,然后firebug-> network,请参阅标题

6 : enable / disable web security in browser like 6: 启用/禁用浏览器中的网络安全

Note : This will be temporary solution that's suitable for only local environment , but as you access same url from browser in other network , CORS issue will appear again. 注意:这是仅适用于本地环境的临时解决方案,但是当您从其他网络的浏览器访问相同的url时,CORS问题将再次出现。

Try this: 尝试这个:

 fabric.Image.fromURL('http://img.fkcdn.com/image/dining-chair/b/g/y/fidcbennywncsach-1-cedar-pine-devdar-home-cherry-white-original-imae9fsfbkxrgebt.jpeg', function (img) {
canvas1.add(img.set({
    left: 50,
    top: 50,
    angle: 30
}));
console.log('CORS enabled + crossOrigin property - DataURL: ', canvas1.toDataURL());}, null,{
crossOrigin: 'Anonymous'});

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM