I've come across a few questions here that gave me some hints for a problem I'm currently having, but none seems to actually fit the bill.
I'm currently working with an angular module, ngCropper (https://github.com/koorgoo/ngCropper) which easily deals with images coming from a file input, but I need it to work also with external URLs. In order to work with external URLs I used the suggestion gave by faheyyy (https://github.com/koorgoo/ngCropper/issues/3) but now I'm dealing with CORS issues and there's no way I can do server changes.
So, I was thinking of loading an image on a hidden HTML element and somehow pick up that data, encode it as Base64 and then I'd be able to use it on the cropper module without needing to create a new GET request (which will create the CORS problem). I've also tried creating a directive that reads the source of an image (the directive is inserted on the img element) and tries putting it on a canvas element, and there gets the dataURL:
.directive('toBase64', function ($document) {
return {
restrict : 'A',
link : function (scope, element, attr) {
element.on('load', function () {
getBase64Image(element);
});
function getBase64Image(element) {
var theImage = new Image();
theImage.src = element.attr("src");
var imageWidth = theImage.width;
var imageHeight = theImage.height;
var canvas = element.siblings('#cropCanvas');
canvas.width = imageWidth;
canvas.height = imageHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(element, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
}
};
});
The directive is still in works, but I'm running into problems with the canvas.getContext("2d") line (it throws undefined).
So far, the threads/answers I found here already deal with the image src being passed as Base64, but I also need it to be working with URLs, and I can't move past this. Could someone, please point me into the right direction? Thank you