What I want to do here is before Dropzone.js send the dropped image to the server, a modal appears with cropper.js (fengyuanchen script) so the user can crop the image, and when image is cropped, send it with Dropzone.js to the server.
So when i change the image src of #cropbox with the function fileToBase64 and replace the image of the cropper with the function cropper('replace'), it keeps showing default.jpg image, instead of the new one uploaded from the user
HTML
<div class="wrapper-crop-box">
<div class="crop-box">
<img src="default.jpg" alt="Cropbox" id="cropbox">
</div>
</div>
JS:
function fileToBase64(file) {
var preview = document.querySelector('.crop-box img');
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
}
$(function() {
Dropzone.options.avtDropzone = {
acceptedFiles: 'image/*',
autoProcessQueue: true,
paramName: 'file',
maxFilesize: 2,
maxFiles: 1,
thumbnailWidth: 200,
thumbnailHeight: 200,
accept: function(file, done) {
fileToBase64(file);
$('#cropbox').cropper('replace', $('#cropbox').attr('src'));
$('.wrapper-crop-box').fadeIn();
done();
},
init: function() {
this.on("addedfile", function(file) {
if (this.files[1]!=null){
this.removeFile(this.files[0]);
}
});
}
};
$('#cropbox').cropper({
aspectRatio: 1 / 1,
resizable: false,
guides: false,
dragCrop: false,
autoCropArea: 0.4,
checkImageOrigin: false,
preview: '.avatar'
});
});