Jquery Dropzone.js change thumbnail width to 100%
You cannot specify a percentage on thumbnailWidth
and thumbnailHeight
. Dropzone uses these values to create the image source to show it as a preview.
But you can leave the thumbnail at the original width and height, setting these values to null
(Note that this can cause a bit of lag with high resolution images) and then use the <img>
width and height attributes to display the image with the size you want and adjusting the .dz-image
container with css.
html:
<div class="dropzone" id="myDropzone"></div>
js:
Dropzone.autoDiscover = false;
Dropzone.options.myDropzone = {
url: "yourUrl",
thumbnailWidth: null,
thumbnailHeight: null,
init: function() {
this.on("thumbnail", function(file, dataUrl) {
$('.dz-image').last().find('img').attr({width: '100%', height: '100%'});
}),
this.on("success", function(file) {
$('.dz-image').css({"width":"100%", "height":"auto"});
})
}
};
var myDropzone = new Dropzone('div#myDropzone');
I needed to accomplish responsive thumbnails with dropzone and this post helped a lot. I also needed to do it without jquery, so here's what I came up with. Figured I'd share if it helps anyone else.
My dropzone init function looks like this:
init: function () {
this.on('thumbnail', function(file, dataUrl) {
var thumbs = document.querySelectorAll('.dz-image');
[].forEach.call(thumbs, function (thumb) {
var img = thumb.querySelector('img');
if (img) {
img.setAttribute('width', '100%');
img.setAttribute('height', '100%');
}
});
}),
this.on('success', function(file) {
var thumbs = document.querySelectorAll('.dz-image');
[].forEach.call(thumbs, function (thumb) {
thumb.style = 'width: 100%; height: auto;';
});
})
}
I'm not a javascript wizard so there is probably a more efficient or better way to do this. Please share!