Skip to content

引入

ts
import {
  saveBase64ImgToFile,
  turnLocalImg2Base64,
  saveRemoteImgToLocal,
  getImgMd5
} from 't-comm';

// 不支持 tree-shaking 的项目
import {
  saveBase64ImgToFile,
  turnLocalImg2Base64,
  saveRemoteImgToLocal,
  getImgMd5
} from 't-comm/lib/node-img/index';

// 只支持 ESM 的项目
import {
  saveBase64ImgToFile,
  turnLocalImg2Base64,
  saveRemoteImgToLocal,
  getImgMd5
} from 't-comm/es/node-img/index';

saveBase64ImgToFile(config)

描述

node环境下,保存base64图片到文件

参数

参数名类型描述
configobject

输入配置

config.imgUrlstring

base64图片Url

config.savePathstring

保存路径,最好是绝对路径

返回: Promise.<string>

去掉前缀的base64图片地址

示例

typescript
saveBase64ImgToFile({
  imgUrl: 'xx',
  savePath: '/test.png'
}).then((base64Data) => {
  console.log(base64Data)
})

turnLocalImg2Base64(savePath)

描述

node环境下,本地图片转为base64

参数

参数名类型描述
savePathstring

本地图片保存路径

返回: string

base64图片地址

示例

typescript
const base64str = turnLocalImg2Base64('/temp.png')

saveRemoteImgToLocal(config)

描述

node环境下,保存网络图片到本地

参数

参数名类型描述
configobject

输入配置

config.imgUrlstring

网络图片地址

config.savePathstring

本地图片保存路径,建议绝对路径

返回: Promise.<object>

请求Promise

示例

typescript
saveRemoteImgToLocal({
  imgUrl: 'xx',
  savePath: './test.png'
}).then(() => {

})

getImgMd5(options)

描述

获取图片md5

参数

参数名类型描述
optionsobject

配置信息

options.savePathstring

本地图片地址,建议绝对路径

返回: Promise.<string>

图片md5值

示例

getImgMd5({ savePath: '/test.png' }).then(md5 => { console.log(md5) })