我使用http://www.dropzonejs.com/实现了一个拖放区域页面。
我遇到了缩放功能的问题。如果图片宽高比不正确,我的图片会被不断裁切。
我想知道两件事:
- 我能否配置dropzone来适应(而非拉伸或裁剪)图片到预览元素中。
- 我能否在图片被拖放后调整预览大小(如查看小型、中型或大型预览)。
我已通过调整css来实现第二个问题,但我想知道是否有更好的方法使用dropzone代码。
如果有人拥有第一个问题的示例,将非常有帮助。谢谢, Matt。
我使用http://www.dropzonejs.com/实现了一个拖放区域页面。
我遇到了缩放功能的问题。如果图片宽高比不正确,我的图片会被不断裁切。
我想知道两件事:
我已通过调整css来实现第二个问题,但我想知道是否有更好的方法使用dropzone代码。
如果有人拥有第一个问题的示例,将非常有帮助。谢谢, Matt。
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,
}
resize
函数。它必须返回一个带有多个属性的对象,正如文档中所描述的那样。以下是根据缩略图尺寸调整预览大小的示例: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;
}
});
trgWidth
和trgHeight
,以便使预览看起来符合您的要求。是的,您可以这样做。要获取未裁剪的缩略图,您需要执行以下2个步骤:
按照以下方式覆盖dropzone css:
.dropzone .dz-preview .dz-image { width: auto !important; height: auto !important; }
resizeInfo.trgWidth = _this.options.thumbnailWidth;
resizeInfo.trgWidth = img.width * (_this.options.thumbnailHeight / img.height);
ctx.drawImage(img, (_ref = resizeInfo.srcX) != null ? _ref : 0, (_ref1 = resizeInfo.srcY) != null ? _ref1 : 0, resizeInfo.srcWidth, resizeInfo.srcHeight, (_ref2 = resizeInfo.trgX) != null ? _ref2 : 0, (_ref3 = resizeInfo.trgY) != null ? _ref3 : 0, resizeInfo.trgWidth, resizeInfo.trgHeight);
ctx.drawImage(img, (_ref = resizeInfo.srcX) != null ? _ref : 0, (_ref1 = resizeInfo.srcY) != null ? _ref1 : 0, img.width, img.height, (_ref2 = resizeInfo.trgX) != null ? _ref2 : 0, (_ref3 = resizeInfo.trgY) != null ? _ref3 : 0, resizeInfo.trgWidth, resizeInfo.trgHeight);
thumbnailWidth: 400,
thumbnailHeight: 400,
resize: function(file) {
var thumbNHeight = this.options.thumbnailHeight;
var thumbNWidth = this.options.thumbnailWidth;
var ratio = 1;
if (file.width > file.height) {
ratio = (file.width / file.height);
thumbNHeight = thumbNHeight / ratio;
} else if (file.height > file.width) {
ratio = (file.height / file.width);
thumbNWidth = thumbNWidth / ratio;
}
var resizeInfo = {
srcX: 0,
srcY: 0,
trgX: 0,
trgY: 0,
srcWidth: file.width,
srcHeight: file.height,
trgWidth: thumbNWidth,
trgHeight: thumbNHeight
};
return resizeInfo;
}
要获取缩略图,请将thumbnailWidth或thumbnailHeight之一设置为null,另一个设置为所需大小:
{
thumbnailWidth: null,
thumbnailHeight: "120"
}
并应用以下 CSS:
.dropzone .dz-preview .dz-image {
width: auto !important;
height: auto !important;
}
.dropzone .dz-preview .dz-image img{
max-width: 100%;
}
在响应式网站上使用img的max-width属性,以便在窄屏幕上缩放图像。
看起来你可以这样设置选项:
{
thumbnailWidth: null,
thumbnailHeight: null
}
而Dropzone会默认调整您的图像大小。