nodejs jimp图片剪裁

1、buffer保存至本地

//引入文件
var fs = require("fs")
//打开文件
var fd = fs.openSync("hello.txt","w")
//向文件内写入内容
fs.writeSync(fd,"传入的内容")
//关闭文件
fs.closeSync(fd)

2、图片剪裁

https://blog.csdn.net/weixin_44402694/article/details/106430395

jimp.read(imgBuffer)
			.then((img) => {
				const topLeftImage = img.clone() // copy jimp对象进行操作
				const topRightImage = img.clone()
				const bottomLeftImage = img.clone()
				const bottomRightImage = img.clone()
				const topLeft = topLeftImage.crop(64, 64, 256, 256)
				topLeft.getBuffer('image/jpeg', (_, buf) => {
					consoel.log(buf)
				})
				const topRight = topRightImage.crop(320, 64, 256, 256)
				topRight.getBuffer('image/jpeg', (_, buf) => {
					consoel.log(buf)
				})
				const bottomLeft = bottomLeftImage.crop(64, 320, 256, 256)
				bottomLeft.getBuffer('image/jpeg', (_, buf) => {
					consoel.log(buf)
				})
				const bottomRight = bottomRightImage.crop(320, 320, 256, 256)
				bottomRight.getBuffer('image/jpeg', (_, buf) => {
					consoel.log(buf)
				})
			})
			.catch(err => {
				console.error(err)
			})

 

posted @ 2023-04-14 22:45  PrintY  阅读(178)  评论(0编辑  收藏  举报