Reputation: 25
I am trying to get Cropper.js working on a URL images from the web.
https://github.com/fengyuanchen/cropperjs
It is working fine when I upload an image from my device but when it comes to an online picture.. I have tons of errors, including:
From origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
The cropper doesnt show up on the image and I gave the Cors error.
Here's my code:
$(function($) {
$("#submit").click(function() {
var selectedFile = $("#imglink").val();
$("#photo").attr("src", selectedFile);
var image = document.getElementById("photo");
console.log(image);
const cropper = new Cropper(image, {
aspectRatio: 16 / 9,
cropBoxResizable: false
});
cropper.crop();
$("#crop-button").on("click", function() {
cropper.getCroppedCanvas().toBlob(function(blob) {
const formData = new FormData();
formData.append("croppedImage", blob);
$.ajax({
type: "POST",
url: "img/index.php",
data: formData,
processData: false,
contentType: false,
success: function(data) {},
error: function(err) {}
});
});
});
});
});
Upvotes: 2
Views: 5915
Reputation: 1223
You can use toDataURL
function of canvas to convert the image into base64 then load it again into image.src
.
var canvas = cropper.getCroppedCanvas()
//replacing the image url with base64 data
image.src = canvas.toDataURL();
canvas.toBlob(function(blob) {
const formData = new FormData();
formData.append("croppedImage", blob);
$.ajax({
type: "POST",
url: "img/index.php",
data: formData,
processData: false,
contentType: false,
success: function(data) {},
error: function(err) {}
});
}
Hope it will works!
Upvotes: 1