当前位置:网站首页>JS -- image to base code, base to file object
JS -- image to base code, base to file object
2022-07-02 00:20:00 【A walking sheep】
【 Picture turn base code 】
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();
}
// call
imgToBase("https://img1.baidu.com/it/u=3382370226,1921570240&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=320")

【base Transcoding File object 】
//base-base code
//name- file 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,
})
}
// call
baseToFile(base,"fileName.jpg")

边栏推荐
猜你喜欢
![[opencv450] hog+svm and hog+cascade for pedestrian detection](/img/55/cdf0bb8231ee59e34c8d5a9d6def23.png)
[opencv450] hog+svm and hog+cascade for pedestrian detection

Leetcode medium question sharing (5)

UDS bootloader of s32kxxx bootloader

数据库--SqlServer详解

The origin of usb-if Association and various interfaces

Niuke - Practice 101 - reasoning clown

RPA tutorial 01: Excel automation from introduction to practice

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速

4. Object mapping Mapstercover
![Data analysis methodology and previous experience summary [notes dry goods]](/img/00/e4c4cf37f1ca9134546f970d800226.png)
Data analysis methodology and previous experience summary [notes dry goods]
随机推荐
I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
ADO. Net SqlDataAdapter object
What is the purpose of ERP project implementation plan?
Windows 7 install MySQL error: 1067
记录一下大文件上传偶然成功偶然失败问题
Vue force cleaning browser cache
【CMake】Qt creator 里面的 cmake 配置
Review data desensitization system
GCC compilation
4. Object mapping Mapstercover
Example explanation: move graph explorer to jupyterlab
heketi 记录
Use the htaccess file to prohibit the script execution permission in the directory
回顾数据脱敏系统
What is ThreadLocal memory leak and how to solve it
【CTF】bjdctf_2020_babystack2
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
How to improve data quality
攻防演练复盘
Is the securities account given by qiniu business school safe? Where can I open an account