[英]Image cropper not working with external images
I am using this image cropper, which is working fine as long as the images I load are actually located in the site map structure itself. 我正在使用这个图像裁剪器,只要我加载的图像实际上位于站点地图结构本身,它就可以正常工作。
However, what I'm trying to do is crop images that are located on an external location through a link like this: ftp://etc/etc/etc.png
但是,我要做的是通过以下链接裁剪位于外部位置的图像:
ftp://etc/etc/etc.png
I'm able to load the images perfectly fine into the canvas, but the moment I try to execute the JavaScript to actually crop the image, nothing happens. 我能够将图像完美地加载到画布中,但是当我尝试执行JavaScript以实际裁剪图像时,没有任何反应。
However this does work when I use a source such as images/img.png
但是,当我使用
images/img.png
这样的源时,这确实有效
Does anyone have a clue how to fix this? 有没有人知道如何解决这个问题?
If you look in your browser console, you will likely find that you are getting a Cross-Origin Resource Sharing policy error similar to this: 如果您查看浏览器控制台,您可能会发现您收到类似于此的跨源资源共享策略错误:
Image from origin ' https://example.com ' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
来自“ https://example.com ”的图像已被跨源资源共享策略阻止加载:请求的资源上没有“Access-Control-Allow-Origin”标头。 Origin ' http://localhost ' is therefore not allowed access.
因此不允许Origin'http:// localhost '访问。
The documentation for this image cropper states: 此图像裁剪器的文档说明:
If you try to start cropper on a cross-origin image, please make sure that your browser supports HTML5 CORS settings attributes, and your image server supports the Access-Control-Allow-Origin option.
如果您尝试在跨源图像上启动裁剪器,请确保您的浏览器支持HTML5 CORS设置属性,并且您的图像服务器支持Access-Control-Allow-Origin选项。
Assuming you own the web server that is serving the image, you may want to look into enabling CORS . 假设您拥有为图像提供服务的Web服务器,您可能需要考虑启用CORS 。
To crop image using canvas with known xy coordinates of image from where you want to crop image : 要使用具有已知xy坐标图像的画布裁剪图像,请从中裁剪图像:
Here either you can use the path defined by you like imgsrc="images/img.png" or any base64 data of image as base64. 在这里,你可以使用你定义的路径,如imgsrc =“images / img.png”或图像的任何base64数据作为base64。
function generateImageThumbnailArrayFromCLientInfo(imgsrc,x,y ClientInfoArray) {
var image = new Image();
image.src = imgsrc;
var imagePieces = [];
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');
context.drawImage(image, x, y, image.width, image.height, 0, 0, canvas.width, canvas.height);
imagePieces=canvas.toDataURL();
return imagePieces;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.