在图像输入尺寸小的情况下,减小图像尺寸将返回更大的图像尺寸

时间:2019-04-03 06:21:58

标签: javascript jquery

我正在尝试在上传之前减小Java脚本中的图像大小,我能够实现将约5mb大小的图像变为1500kb大小,但是当我使用小图像(如15kb大小的图像)时,它会返回170kb大小的图像,另外该图像返回的值是在chrome浏览器中返回旋转后的结果,请问如何解决在所有情况下将图像尺寸减小为较小并在chrome中处理旋转后的结果,特别是因为它在firefox上很完美

for (var i = 0; i < fileUpload.files.length; i++) {
    var file = fileUpload.files[i];
    if (file.type.match('image')) {
        var reader = new FileReader();
        reader.onload = function(e) {
            var fileData = e.target.result;
            var canvasSettings = {
                width: 0,
                height: 0,
                adjustedHeight: Number.MAX_VALUE,
                adjustedWidth: Number.MAX_VALUE,
                img: new Image()
            };
            canvasSettings.img.src = e.target.result;
            canvasSettings.img.onload = function() {
                canvasSettings.height = canvasSettings.img.height;
                canvasSettings.width = canvasSettings.img.width;
                var canvas = $("<canvas/>").get(0);
                canvas.width = canvasSettings.width;
                canvas.height = canvasSettings.height;
                var context = canvas.getContext('2d');
                context.drawImage(canvasSettings.img, 0, 0,
                    canvasSettings.width, canvasSettings.height);
                fileData = canvas.toDataURL();
            }

        }
    }

}

0 个答案:

没有答案