1

I try to use sharp with formidable in order to resize my image and change the format but it does not work as expected.

The created file is not with the correct format and the correct extension.

My code :

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);
  }
});

With this code, I got this file :

  • 0e3deab581ff7c29f146bef8ac679fa7c603dad71677965394628.jpg (same height, width and extension as the original file)
tonymx227
  • 5,293
  • 16
  • 48
  • 91

1 Answers1

0

You forgot the line that saves the result of sharp resize into a new file

Walle Cyril
  • 3,087
  • 4
  • 23
  • 55