I want to basically compress images uploaded (client-side) and then attach the src to my id (of html). The code of the function is as below:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
console.log("e.target.result",e.target.result);
var imageEle = new Image();
imageEle.src = e.target.result;
imageEle.onload = function() {
var cvs = document.createElement('canvas');
cvs.width = imageEle.naturalWidth;
cvs.height = imageEle.naturalHeight;
var ctx = cvs.getContext("2d").drawImage(imageEle,0,0);
var newImageData = cvs.toDataURL('image/jpeg',0.5);
$('#uploadedImage').attr('src', newImageData);
};
};
reader.readAsDataURL(input.files[0]);
}
}
This should work fine, but what happens is the uploaded image after process appears to be a black box. Now I have read alot of similar questions, but in relation to compression I am not able to fix this issue. Also this works good with files (jpeg images) less than 500 Kb, but for real files that are 1 MB plus it gives a black box as data uri. It would be awesome if somebody could help me with this.
Thanks, Vaibhav