实际上,只有当您明确指定thumbnailWidth 和thumbnailHeight 两个选项时,预览似乎才会被裁剪。否则,如果您只指定一个或不指定缩略图尺寸,则整个图像将根据指定的选项按比例调整大小thumbnailWidth 或 thumbnailHeight。
示例,使用 1200x800 图像源:
// Resized preview (400x267)
var dp = new Dropzone(document.getElementById('dp'), {
thumbnailWidth: 400,
thumbnailHeight: null
}
// Resized preview (600x400)
var dp = new Dropzone(document.getElementById('dp'), {
thumbnailWidth: null,
thumbnailHeight: 400,
}
// Croped preview (400x400)
var dp = new Dropzone(document.getElementById('dp'), {
thumbnailWidth: 400,
thumbnailHeight: 400,
}
但是,如果您不知道源图像的比例并且需要使您的预览适合大小的 div,那么请使用 dropzone.js 的resize 函数。它必须返回一个具有多个属性的对象,如the documentation 中所述。以下是根据缩略图尺寸调整预览大小的示例:
var dp = new Dropzone(document.getElementById('myDropzone'), {
// Your dropzone options here...
thumbnailWidth: 400,
thumbnailHeight: 400,
resize: function(file) {
var resizeInfo = {
srcX: 0,
srcY: 0,
trgX: 0,
trgY: 0,
srcWidth: file.width,
srcHeight: file.height,
trgWidth: this.options.thumbnailWidth,
trgHeight: this.options.thumbnailHeight
};
return resizeInfo;
}
});
这将导致拉伸预览。
但当然,您可以根据您的源图像尺寸、您的 div 尺寸或您想要的任何东西找出trgWidth 和trgHeight,以使预览看起来像您想要的那样。