当前位置:网站首页>JS——图片转base码 、base转File对象
JS——图片转base码 、base转File对象
2022-07-02 00:15:00 【一只漫步前行的羊】
【图片转base码】
function imgToBase(url){
let img = document.createElement("img")
img.src = url
img.setAttribute("crossOrigin",'Anonymous')
let canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
let ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, img.width, img.height);
return canvas.toDataURL();
}
//调用
imgToBase("https://img1.baidu.com/it/u=3382370226,1921570240&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=320")

【base码转File对象】
//base-base码
//name-文件名
function baseToFile(base, name) {
var arr = base.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], name, {
type: mime,
})
}
//调用
baseToFile(base,"fileName.jpg")

边栏推荐
- Learn online case practice
- How excel opens CSV files with more than one million lines
- Linux centos7 installation Oracle11g super perfect novice tutorial
- [cmake] cmake configuration in QT Creator
- MySQL: the difference between insert ignore, insert and replace
- 电商RPA机器人,助力品牌电商抢立流量高点
- .env.xxx 文件,加了常量,卻undefined
- [QT] test whether QT can connect to the database
- Dongge cashes in and the boss retires?
- [QT] qtcreator uninstall and installation (abnormal state)
猜你喜欢

Digital transformation has a long way to go, so how to take the key first step

ThreadLocal内存泄漏是什么,怎么解决

LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电

The origin of usb-if Association and various interfaces

Shell process control
![Jielizhi, production line assembly link [chapter]](/img/1d/d1736fad33c428e61f450aad512ce0.png)
Jielizhi, production line assembly link [chapter]
![Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]](/img/3a/cced28a2eea9f9a0d107baabd01119.png)
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]

LeetCode中等题题分享(5)

Using multithreaded callable to query Oracle Database

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
随机推荐
SQL Server 安装指南
Learn online case practice
Jielizhi Bluetooth headset quality control and production skills [chapter]
[QT] test whether QT can connect to the database
Niuke - Practice 101 - reasoning clown
智能运维实战:银行业务流程及单笔交易追踪
How excel opens CSV files with more than one million lines
Timer和ScheduledThreadPoolExecutor的区别
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
一个实习生的CnosDB之旅
Use vb Net to convert PNG pictures into icon type icon files
【模板】自适应辛普森积分
Shell process control
使用VB.net将PNG图片转成icon类型图标文件
Is it safe to buy funds in a securities account? Where can I buy funds
如何提升数据质量
Pytorch learning record
What is the purpose of ERP project implementation plan?
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
Vue force cleaning browser cache