使用强大的尖锐

问题描述 投票:0回答:0

我尝试使用 sharp with formidable 来调整我的图像大小并更改格式,但它没有按预期工作。

ceeated 文件的格式和扩展名不正确。

我的代码:

form.on('fileBegin', function(name, file) {
  if (file.name !== '') {
    var ext = file.name.match(/\.(jpg|jpeg|JPG|png|webp|pdf|ico|ttf|otf)$/i)[0],
        newFileName = crypto.createHmac('sha1', 'test')
          .update(file.name)
          .digest('hex') + new Date()
          .getTime();
    file.path = './public/uploads/' + newFileName + ext;
  }
});

form.on('file', async function(name, file) {
  if (file.name !== '') {
    var fileBuffer = fs.readFileSync(file.path),
        sharpImage = sharp(fileBuffer);

    sharpImage.resize({500, 500}).toFormat(sharp.format.webp);
  }
});

有了这段代码,我得到了这个文件:

  • 0e3deab581ff7c29f146bef8ac679fa7c603dad71677965394628.jpg
    (与原始文件相同的高度、宽度和扩展名)
sharp formidable
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.