几年前我解决了这个问题并将我的解决方案上传到 github 作为https://github.com/rossturner/HTML5-ImageUploader
robertc 的答案使用了Mozilla Hacks 博客文章中提出的解决方案,但是我发现当调整到不是 2:1(或其倍数)的比例时,图像质量非常差。我开始尝试不同的图像大小调整算法,尽管大多数算法最终都很慢,或者质量也不是很好。
最后,我想出了一个我相信执行速度很快并且性能也相当不错的解决方案——因为 Mozilla 解决方案从一个画布复制到另一个画布可以快速工作,并且在 2:1 的比例下不会损失图像质量,给定目标x像素宽和y像素高,我使用这种画布调整大小的方法,直到图像介于x和 2 x以及y和 2 y之间。在这一点上,我然后转向算法图像调整大小,以实现将大小调整到目标大小的最后“步骤”。在尝试了几种不同的算法后,我选择了双线性插值法,该插值法取自不再在线但可通过 Internet 档案访问的博客,这给出了很好的结果,这是适用的代码:
ImageUploader.prototype.scaleImage = function(img, completionCallback) {
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0, canvas.width, canvas.height);
while (canvas.width >= (2 * this.config.maxWidth)) {
canvas = this.getHalfScaleCanvas(canvas);
}
if (canvas.width > this.config.maxWidth) {
canvas = this.scaleCanvasWithAlgorithm(canvas);
}
var imageData = canvas.toDataURL('image/jpeg', this.config.quality);
this.performUpload(imageData, completionCallback);
};
ImageUploader.prototype.scaleCanvasWithAlgorithm = function(canvas) {
var scaledCanvas = document.createElement('canvas');
var scale = this.config.maxWidth / canvas.width;
scaledCanvas.width = canvas.width * scale;
scaledCanvas.height = canvas.height * scale;
var srcImgData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);
var destImgData = scaledCanvas.getContext('2d').createImageData(scaledCanvas.width, scaledCanvas.height);
this.applyBilinearInterpolation(srcImgData, destImgData, scale);
scaledCanvas.getContext('2d').putImageData(destImgData, 0, 0);
return scaledCanvas;
};
ImageUploader.prototype.getHalfScaleCanvas = function(canvas) {
var halfCanvas = document.createElement('canvas');
halfCanvas.width = canvas.width / 2;
halfCanvas.height = canvas.height / 2;
halfCanvas.getContext('2d').drawImage(canvas, 0, 0, halfCanvas.width, halfCanvas.height);
return halfCanvas;
};
ImageUploader.prototype.applyBilinearInterpolation = function(srcCanvasData, destCanvasData, scale) {
function inner(f00, f10, f01, f11, x, y) {
var un_x = 1.0 - x;
var un_y = 1.0 - y;
return (f00 * un_x * un_y + f10 * x * un_y + f01 * un_x * y + f11 * x * y);
}
var i, j;
var iyv, iy0, iy1, ixv, ix0, ix1;
var idxD, idxS00, idxS10, idxS01, idxS11;
var dx, dy;
var r, g, b, a;
for (i = 0; i < destCanvasData.height; ++i) {
iyv = i / scale;
iy0 = Math.floor(iyv);
// Math.ceil can go over bounds
iy1 = (Math.ceil(iyv) > (srcCanvasData.height - 1) ? (srcCanvasData.height - 1) : Math.ceil(iyv));
for (j = 0; j < destCanvasData.width; ++j) {
ixv = j / scale;
ix0 = Math.floor(ixv);
// Math.ceil can go over bounds
ix1 = (Math.ceil(ixv) > (srcCanvasData.width - 1) ? (srcCanvasData.width - 1) : Math.ceil(ixv));
idxD = (j + destCanvasData.width * i) * 4;
// matrix to vector indices
idxS00 = (ix0 + srcCanvasData.width * iy0) * 4;
idxS10 = (ix1 + srcCanvasData.width * iy0) * 4;
idxS01 = (ix0 + srcCanvasData.width * iy1) * 4;
idxS11 = (ix1 + srcCanvasData.width * iy1) * 4;
// overall coordinates to unit square
dx = ixv - ix0;
dy = iyv - iy0;
// I let the r, g, b, a on purpose for debugging
r = inner(srcCanvasData.data[idxS00], srcCanvasData.data[idxS10], srcCanvasData.data[idxS01], srcCanvasData.data[idxS11], dx, dy);
destCanvasData.data[idxD] = r;
g = inner(srcCanvasData.data[idxS00 + 1], srcCanvasData.data[idxS10 + 1], srcCanvasData.data[idxS01 + 1], srcCanvasData.data[idxS11 + 1], dx, dy);
destCanvasData.data[idxD + 1] = g;
b = inner(srcCanvasData.data[idxS00 + 2], srcCanvasData.data[idxS10 + 2], srcCanvasData.data[idxS01 + 2], srcCanvasData.data[idxS11 + 2], dx, dy);
destCanvasData.data[idxD + 2] = b;
a = inner(srcCanvasData.data[idxS00 + 3], srcCanvasData.data[idxS10 + 3], srcCanvasData.data[idxS01 + 3], srcCanvasData.data[idxS11 + 3], dx, dy);
destCanvasData.data[idxD + 3] = a;
}
}
};
这会将图像缩小到 的宽度config.maxWidth
,同时保持原始纵横比。在开发时,除了主要的桌面浏览器(IE9+、Firefox、Chrome)之外,它还可以在 iPad/iPhone Safari 上运行,所以我预计它仍然可以兼容,因为现在 HTML5 得到了更广泛的采用。请注意,canvas.toDataURL() 调用采用 mime 类型和图像质量,这将允许您控制质量和输出文件格式(如果您愿意,可能与输入不同)。
这没有涉及的唯一一点是维护方向信息,在不知道此元数据的情况下,图像被调整大小并按原样保存,丢失图像中用于方向的任何元数据,这意味着在平板设备上拍摄的图像“倒置”是这样渲染,尽管它们会在设备的相机取景器中翻转。如果这是一个问题,这篇博文有一个很好的指南和代码示例,说明如何完成此操作,我确信可以将其集成到上述代码中。