将圆形图像转换为矩形图像Node.js Sharp

问题描述

我当时使用angular-croppie-module上传用户的头像图片,但现在我也需要使用矩形图片。我可以为新用户上传矩形图像,但对于旧用户,我需要使用矩形图像更新旧的头像图像(即圆形图像)。我正在使用夏普。这是我到目前为止尝试过的。

const sharp = require('sharp');

// async/await
async function run () {
  try {
    const imagePathCircle = './circle/avatar.jpg';
    const thumbPathRectangle = './rectangle/avatar.jpg';
   await sharp(imagePathCircle)
          .resize({
            width: 15,height: 15,fit: sharp.fit.cover,position: sharp.strategy.entropy
          })
          .toFile(thumbPathRectangle)
          .then(() => console.info(`Outputed file ${thumbPathRectangle}`));
    console.log('Done!')
  } catch (err) {
    console.error(err)
  }
}
 
run()

,但是上面的代码仅调整图像的大小,从而降低了图像的质量。有什么方法可以将图像更新为矩形图像而不改变质量?

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)