Jimp是一个完全用JavaScript编写、零原生依赖的Node图像处理库
安装 jimp 包
npm install jimp@1.6.0
引入包
const { Jimp, JimpMime } = require('jimp');
var base64 = 'abcde' // base64不包含MIME信息前缀
var buff = Buffer.from(base64, 'base64');
var image = await Jimp.read(buff);
var image = await Jimp.read('filepath');
图片裁剪
const width = 500
const height = 500
await image.cover({
w: width,h: height
})
await image.contain({
w: width,h: height
})
读取图片的base64
const base64 = await image.getBase64(JimpMime.png)
保存图片到服务器的指定路径
const nowtime=new Date().getTime()+'_'+parseInt(Math.random()*1000)
const filename=`uploads/${nowtime}.png`
await image.write(filename)