上传图片压缩功能

        // 压缩方式
                var $img = new Image();
                $img.src = window.URL.createObjectURL(fs);
                $img.onload = function() {
                    var width = $img.width,
                        height = $img.height;
                    if (fs.size > 500 * 1024) {
                        var scale = width / height;
                        if (width > height) {
                            width = 800;
                            height = parseInt(width / scale);
                        } else {
                            height = 800;
                            width = parseInt(height * scale);
                        }
                    }

                    var canvas = $("<canvas></canvas>");
                    var ctx = canvas[0].getContext('2d');
                    canvas.attr({
                         width,
                        height: height
                    });
                    ctx.drawImage($img, 0, 0, width, height);

                    var proBase64 = canvas[0].toDataURL('image/jpg');

                    proPlus_button.css("background-image", "url(" + proBase64 + ")");
原文地址:https://www.cnblogs.com/zccfun/p/6054401.html