当前位置:网站首页>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")
边栏推荐
- Material design component - use bottomsheet to show extended content (I)
- Vue force cleaning browser cache
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- [embedded system course design] a single key controls the LED light
- Heketi record
- . env. XXX file, with constant, but undefined
- 13 MySQL constraint
- Resumption of attack and defense drill
- Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
- Which securities company is the best to open a stock account? Is there a security guarantee
猜你喜欢
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
数据库--SqlServer详解
Halcon knowledge: an attempt of 3D reconstruction
leetcode96不同的二叉搜索樹
关联性——组内相关系数
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
牛客-练习赛101-推理小丑
【QT】Qt 使用MSVC2017找不到编译器的解决办法
SQL Server Installation Guide
leetcode96不同的二叉搜索树
随机推荐
LeetCode中等题题分享(5)
B tree and b+tree of MySQL
13 MySQL constraint
Relatively easy to understand PID understanding
使用多线程Callable查询oracle数据库
启牛商学院给的证券账户安不安全?哪里可以开户
Jielizhi Bluetooth headset quality control and production skills [chapter]
Jielizhi, production line assembly link [chapter]
回顾数据脱敏系统
MySQL: the difference between insert ignore, insert and replace
Jielizhi, production line assembly link [chapter]
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
UDS bootloader of s32kxxx bootloader
[embedded system course design] a single key controls the LED light
二叉搜索树的创建,查找,添加,删除操作
Why does blocprovider feel similar to provider?
Graduation season is both a farewell and a new beginning
PWN attack and defense world cgpwn2
使用VB.net将PNG图片转成icon类型图标文件
如何提升数据质量