url 转图片流 / url转base64 / base64 转图片流 / base64转url
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); })()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了