I'm trying to work with jCrop
and canvas
.
Instead of sending the image to the server and back again with a cropped image, I'm trying to crop it directly, using canvas, because with the result, I transform the crop in a base64 image and send to the server.
I have an example JSFiddle.
I crop the image then convert in canvas but I cant transform image in base64. The below error appears:
"SecurityError: The operation is insecure."
I have tried with..
console.log(canvas.toDataURL());
What am I doing wrong?
In general, this problem is due to CORS (cross-origin resource sharing) requirements not being fulfilled.
To avoid this you can either:
crossOrigin
property to the imageIn addition:
file://
protocol but a light local server (ie. Mongoose)If not you will get an security error every time you use toDataURL
(or getImageData
).
About requesting CORS usage from external servers: you can only request such use but it's up to the server to grant the permission or not.
To request either:
<img src="..." crossOrigin="anonymous" >
or from JavaScript:
var img = new Image;
img.onload = handleOnLoad;
img.crossOrigin = 'anonymous';
img.src = '...';
If denied the image won't load at all.