js图片压缩

懒需要解释吗?

 function compressedPicture (url, callback) {
            var canvas = document.createElement('canvas');
            var ctx = canvas.getContext('2d');
            var img = new Image;
            img.onload = function(){
                console.log(img.width);
                var width = img.width;
                var height = img.height;
                // 按比例压缩4倍
                var rate = (width < height ? width / height : height / width) / 4;
                canvas.width = width * rate;
                canvas.height = height * rate;
                ctx.drawImage(img, 0, 0, width, height, 0, 0, width * rate, height * rate);
                var dataURL = canvas.toDataURL('image/jpeg');
                callback.call(this, dataURL);
                canvas = null;
                console.log(dataURL);
            };
            img.src = url
        }
 //调用
 this.compressedPicture(这里放base64的图, _ => {
                console.log(_);
 })

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容