本文共 680 字,大约阅读时间需要 2 分钟。
安装Jimp库
在项目根目录中打开终端,执行以下命令安装Jimp库:
npm install --save jimp读取本地图片并进行剪切
如果需要处理本地图片,可以按照以下方式实现:
jimp.read('图片路径', function(err, img) { if (err) throw err; img.crop(x坐标起点, y坐标起点, width, height) .write('处理后的图片路径');});读取HTTP图片并转换为Buffer数据
如果需要处理远程图片,可以通过以下方法获取并转换为Buffer数据:
const http = require('http'); const options = { headers: { 'User-Agent': 'Mozilla/5.0' } }; http.get('图片URL', options, function(response) { response.setEncoding('binary'); let imgData = ''; response.on('data', function(chunk) { imgData += chunk; }); response.on('end', function() { const imgBuffer = new Buffer.from(imgData); // 根据需求进行图片处理 });
转载地址:http://wijfk.baihongyu.com/