export function zipImg(file: File) {
  return new Promise((resolve) => {
    if (file && (file.size / 1024 > 500 || file.type !== 'image/gif')) {
      let img = new Image();
      img.src = URL.createObjectURL(file);
      let cvs = document.createElement('canvas');
      let maxRatio = 0.75; // 大图比率
      let minRatio = 0.8; // 小图比率
      let imgQulity = 0.2; // 图像质量
      img.onload = async function () {
        let ratio =
          img.naturalWidth > 1000 || img.naturalHeight > 1000
            ? maxRatio
            : minRatio;
        cvs.width = img.naturalWidth * ratio;
        cvs.height = img.naturalHeight * ratio;
        let ctx: any = cvs.getContext('2d');
        ctx.drawImage(img, 0, 0, cvs.width, cvs.height);
        // 压缩后新图的 base64
        let zipBase64 = cvs.toDataURL('image/jpeg', imgQulity);
        let add = base642File(zipBase64);
        resolve(add);
      };
    } else {
      resolve(file);
    }
  });
}

// base64转图片
export function dataURLtoFile(dataurl: any, filename: any, mime: any) {
  return new Promise((resolve) => {
    let arr = dataurl.split(';base64,');
    let bstr = atob(arr[1]);
    let n = bstr.length;
    let u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    let data = new File([u8arr], `${filename}`, {
      type: mime,
    });
    resolve(data);
  });
}

export function base642File(base64: any) {
  const arr = base64.split(',');
  const mime = arr[0].match(/:(.*?);/)[1];
  const bstr = atob(arr[1]);
  let n = bstr.length;
  const u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
}

export const ChangeBlob = (value: string) => {
  //将字符串 转换成 Blob 对象
  let blob = new Blob([value], {
    type: 'text/plain',
  });
  return blob;
};
