base64图片大小计算

2019-07-19  本文已影响0人  云翼飞

字节数转化为kb,MB,GB

  function change(limit) {
    var size = "";
    if (limit < 0.1 * 1024) { //小于0.1KB,则转化成B
      size = limit.toFixed(2) + "B"
    } else if (limit < 0.1 * 1024 * 1024) { //小于0.1MB,则转化成KB
      size = (limit / 1024).toFixed(2) + "KB"
    } else if (limit < 0.1 * 1024 * 1024 * 1024) { //小于0.1GB,则转化成MB
      size = (limit / (1024 * 1024)).toFixed(2) + "MB"
    } else { //其他转化成GB
      size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB"
    }

    var sizeStr = size + ""; //转成字符串
    var index = sizeStr.indexOf("."); //获取小数点处的索引
    var dou = sizeStr.substr(index + 1, 2) //获取小数点后两位的值
    if (dou == "00") { //判断后两位是否为00,如果是则删除00                
      return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
    }
    return size;
  }

base64图片大小计算(kb)

  //获取base64图片大小,返回kb数字
  function showSize(base64url) {
    //把头部去掉
    var str = base64url.replace('data:image/png;base64,', '');
    // 找到等号,把等号也去掉
    var equalIndex = str.indexOf('=');
    if(str.indexOf('=')>0) {
        str=str.substring(0, equalIndex);
    }
    // 原来的字符流大小,单位为字节
    var strLength=str.length;
    // 计算后得到的文件流大小,单位为字节
    var fileLength=parseInt(strLength-(strLength/8)*2);
    // 由字节转换为kb
    var size = "";
    size = (fileLength / 1024).toFixed(2);
    var sizeStr = size + ""; //转成字符串
    var index = sizeStr.indexOf("."); //获取小数点处的索引
    var dou = sizeStr.substr(index + 1, 2) //获取小数点后两位的值
    if (dou == "00") { //判断后两位是否为00,如果是则删除00                
      return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
    }
    return size;
  }
  var size = showSize(imagebase64)
 var imagebase64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAGFUlEQVR4AdWVA5Br8RXGb227fapWT3FS226fbdtY89lexc7DGknWNrLPdj2q269nT+dmbzNBjcz8v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