我正在开发一个应用程序,其中用户将头像上传到服务器,然后要求他裁剪图像。我只需要从流中获取图像大小,然后按百分比裁剪它。
这是我当前的代码,但到目前为止还没有工作:
router.use('/', (req, res) => {
//Crop parameters, these will be providen by user directly
const parameters = {
left: 0.5, //crop from left percentage
top: 0.2, //crop from top percentage
size: 0.5 //size percentage (extracted from width)
}
const fileStream = s3.getObject({
Bucket: '<my-bucket-name>',
Key: fileKey
}).createReadStream();
let imageInfo;
const infoTransformer = sharp().on('info', info => {
imageInfo = info;
})
const cropTransformer = sharp().extract({
left: parameters.left * imageInfo.width,
top: parameters.top * imageInfo.height,
width: parameters.size * imageInfo.width,
height: parameters.size * imageInfo.width
});
return fileStream.pipe(infoTransformer).pipe(cropTransformer).pipe(res);
})
我究竟做错了什么?