js 图片压缩

imgToLessen = (url,defaultWidth)=>{
        //建一个画布
        let cvs = document.createElement('canvas');
        let ctx = cvs.getContext('2d');
        let img = new window.Image();
        img.src = url;
        let width = img.width, height = img.height;
        //压缩后图片的默认大小
        let bili=height/width;
        cvs.width = defaultWidth;
        cvs.height = defaultWidth*bili;
        ctx.drawImage(img, 0, 0, cvs.width, cvs.height);
        //获取压缩图片后的BASE64
        return cvs.toDataURL('image/jpeg',0.5);
    };

你可能感兴趣的:(js)