首页 > 其他分享 >url 转图片流 / url转base64 / base64 转图片流 / base64转url

url 转图片流 / url转base64 / base64 转图片流 / base64转url

时间:2022-10-27 10:56:43浏览次数:63  
标签:const url image base64 file new 图片

1.url 转图片流

// url 转 图片流
const urlToFile = (url, imageName) => {
  return new Promise((resolve, reject) => {
    let blob = null;
    const xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.setRequestHeader('Accept', 'image/png');
    xhr.responseType = "blob";
    xhr.onload = () => {
      blob = xhr.response;
      let imgFile = new File([blob], imageName, { type: 'image/png' });
      resolve(imgFile);
    };
    xhr.onerror = (e) => {
      reject(e);
    };
    xhr.send();
  });
};
(async () => {
  const baseurl = 'https://img1.baidu.com/it/u=3622442929,3246643478&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500';
  const file = await urlToFile(baseurl);
  console.log('file', file);
})()

2. url转base64

// url 转 base64
const urlToBase64 =  (url) => {
  return new Promise((resolve, reject) => {
    const image = new Image();  
    image.setAttribute('crossOrigin', 'anonymous');
    image.src = url;  
    image.onload = function(){  
      const canvas = document.createElement("canvas");  
      canvas.width = image.width;  
      canvas.height = image.height;  
      const ctx = canvas.getContext("2d");  
      ctx.drawImage(image, 0, 0, image.width, image.height);  
      const ext = image.src.substring(image.src.lastIndexOf(".")+1).toLowerCase();
      const base64 = canvas.toDataURL("image/"+ext);  
      resolve(base64);
    }
    image.onerror = (e) => {
      reject(e);
    };
  });
}
(async () => {
  const baseurl = 'https://img1.baidu.com/it/u=3622442929,3246643478&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500';
  const base64 = await urlToBase64(baseurl);
  console.log('base64', base64);
})()

3.base64 转图片流

// base64 转 图片流
const base64ToFile = (base64, filename = 'file') => {
  const arr = base64.split(',');
  const mime = arr[0].match(/:(.*?);/)[1];
  const suffix = mime.split('/')[1];
  const bstr = atob(arr[1]);
  let n = bstr.length;
  const u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  };
  const file = new File([u8arr], `${filename}.${suffix}`, {
    type: mime
  });
  return file;
}

(async () => {
  const baseurl = 'https://img1.baidu.com/it/u=3622442929,3246643478&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500';
  const base64 = await urlToBase64(baseurl);
  console.log('base64', base64);

  const file = await base64ToFile(base64);
  console.log('file', file);

})()

 

4.base64转url

// base64 转 图片url
const base64ToUrl = (base64, filename = 'file') => {
  const arr = base64.split(',');
  const mime = arr[0].match(/:(.*?);/)[1];
  const suffix = mime.split('/')[1];
  const bstr = atob(arr[1]);
  let n = bstr.length;
  const u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  };
  const file = new File([u8arr], `${filename}.${suffix}`, {
    type: mime
  });
  return URL.createObjectURL(file);
}

(async () => {
  const baseurl = 'https://img1.baidu.com/it/u=3622442929,3246643478&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500';
 
  const base64 = await urlToBase64(baseurl);
  console.log('base64', base64);

  const url = await base64ToUrl(base64);
  console.log('url', url);
})()

 

标签:const,url,image,base64,file,new,图片
From: https://www.cnblogs.com/webtown/p/16831400.html

相关文章

  • 图片瀑布流效果
    body,div,p{padding:0px;margin:0px}#content{padding:0px;margin:0pxauto;/*左右居中上下为0*/width:80%;/*宽为80%*/}#content.no......
  • 小程序图片安全检查
    借助临时CDN传递大数据到云函数实现图片安全检测最近在重构小程序恋爱小清单,在用云函数做图片的安全检测时报了一个错:cloud.callFunction:failError:dataexceedm......
  • 使用matlab根据液体扩散图片分析其对应的等浓度线
    目录一、理论基础二、核心程序三、仿真测试结果作者ID:fpga和matlabCSDN主页:https://blog.csdn.net/ccsss22?type=blog擅长技术:1.无线基带,无线图传,编解码2.机器视觉......
  • 微信小程序富文本内容中的图片处理
    后台上传图片,大小各异,为了让图片在微信小程序更好的的显示,进行了以下处理:找到内容中的图片,去除原有的宽高属性,给图片加上的宽度最大100%,高度自适应的样式formatRichText......
  • 什么是Base64 编码,Base64 编码有哪些优缺点
    很多朋友在工作中,可能经常会用到Base64编码。Base64编码是网络上很常见的用于8Bit字节码的编码方式之一,那么,大家知道为什么要使用Base64编码,Base64编码有哪些优缺点呢?下面......
  • 什么是Base64 编码,Base64 编码有哪些优缺点
    很多朋友在工作中,可能经常会用到Base64编码。Base64编码是网络上很常见的用于8Bit字节码的编码方式之一,那么,大家知道为什么要使用Base64编码,Base64编码有哪些优缺点呢?下面......
  • 什么是Base64 编码,Base64 编码有哪些优缺点
    很多朋友在工作中,可能经常会用到Base64编码。Base64编码是网络上很常见的用于8Bit字节码的编码方式之一,那么,大家知道为什么要使用Base64编码,Base64编码有哪些优缺点呢?下面......
  • keras使用预训练模型inception_v3识别图片
    本文使用keras中inception_v3预训练模型识别图片。结合官方源码,如下内容。其中,数据输入借助opencv-python,程序运行至​​model=InceptionV3()​​​时按需(如果不存在就)下载......
  • 图片跑马灯;一行图片永远轮播
      有N张图片无限滚动轮播。代码如下。       1<divclass="partnerCont">2<divclass="cont">3<divclass="list">4......
  • 图片中的热点区域
    图片上添加热点这个功能貌似很少用了,今天偶然想起了大学的课程,竟然忘记怎么用了,后来度娘一下,重新拾起!其实很简单,上教程这里只需要三个html标签分别是img map area......