当前位置:网站首页>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")

边栏推荐
- How excel opens CSV files with more than one million lines
- 关联性——组内相关系数
- mysql之B tree 以及 B+tree
- Pytorch learning record
- Use pair to do unordered_ Key value of map
- Flow control statement of SQL data analysis [if, case... When detailed]
- 在证券账户上买基金安全吗?哪里可以买基金
- 使用htaccess文件禁止目录里的脚本执行权限
- PHP reads ini or env type configuration
- 挖财学堂开户打新债安全可靠嘛?
猜你喜欢
![Jielizhi, production line assembly link [chapter]](/img/f8/20c41ffe9468d59bf25ea49f73751e.png)
Jielizhi, production line assembly link [chapter]

LDR6035智能蓝牙音响可对手机设备持续充放电方案

4. Object mapping Mapstercover

Shell process control

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

Difficult to get up syndrome (bit by bit greed)
![[embedded system course design] a single key controls the LED light](/img/c9/076618208bbab0b95faa5a7e644a07.png)
[embedded system course design] a single key controls the LED light

时间复杂度与空间复杂度

如何提升数据质量
![Data analysis methodology and previous experience summary [notes dry goods]](/img/00/e4c4cf37f1ca9134546f970d800226.png)
Data analysis methodology and previous experience summary [notes dry goods]
随机推荐
PyTorch学习记录
How to solve the image pop-up problem when pycharm calls Matplotlib to draw
Iota in golang
leetcode96不同的二叉搜索樹
Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development
Jielizhi, production line assembly link [chapter]
【QT】QtCreator卸载与安装(非正常状态)
S32Kxxx bootloader之UDS bootloader
使用htaccess文件禁止目录里的脚本执行权限
Regular expression collection
使用多线程Callable查询oracle数据库
ADO. Net SqlConnection object usage summary
Selectively inhibiting learning bias for active sampling
回顾数据脱敏系统
Practical calculation of the whole process of operational amplifier hysteresis comparator
vue 强制清理浏览器缓存
数据库--SqlServer详解
SQL数据分析之子查询的综合用法和案例题【耐心整理】
How to improve data quality
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】