/**
* 异步压缩
* @param {*} imgfile 上传的文件
* @returns urlBase64 处理后的base64字符串
*/
const compressImgSync = (imgfile) => {
return new Promise((resolve, reject) => {
let img = new Image()
let cvs = document.createElement('canvas')
let file = imgfile.file // 获取到文件对象
// 上传的图片大于400KB时才压缩
if (file && file.size / 1024 > 400) {
let reader = new FileReader()
reader.readAsDataURL(file) // 转成 base64 编码
reader.onload = function (e) {
let naturalBase64 = e.target.result // 获取 base64 编码,这是原图的
img.src = naturalBase64
img.onload = function () {
let ratio = img.naturalWidth / img.naturalHeight // 获取原图比例,为了等比压缩
cvs.width = 400 // 设定canvas的宽度