javascript - canvas 跨域
天蓬老师
天蓬老师 2017-04-10 17:03:47
[JavaScript讨论组]

我这个下面的canvas.toDataURL报错,是因为上面的picUrl图片地址跨域,有什么办法解决?

            img.src = picUrl;//图片跨域了
            
            img.onload = function(){
                
                var ctx = canvas.getContext("2d");
                ctx.clearRect(0,0,canvas.width,canvas.height);
                if(img.height > 800){
                   img.width *= 800 / img.height;
                   img.height = 800;
                }                
                if(img.width > 800){
                   img.height *= 800 / img.width;
                   img.width = 800;
                }
                canvas.width = img.width;
                canvas.height = img.height;
                ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
                var imgUrl = canvas.toDataURL("image/jpeg");
            }
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(2)
怪我咯

想办法解决跨域问题,两种思路:

  • 1、修改图片域名为同域

  • 2、为图片服务添加CROS(跨域)支持,具体方法是在图片的返回的header中添加Access-Control-Allow-Origin:*,同时在请求图片时,为image元素设置crossOrigin="" 属性设置为空字符串。

PHPz

同上,你还可以在自己的不跨域的服务器里加个接口,让他去抓图片,前端用的图片访问这个接口

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号