I'm trying to rotate images on my computer using Sharp and Nodejs but every single rotation causes a small increase to the file size, to the point I've got one image 10x the size of its original (from 50kb to 500kb) just by rotating it.
I've noticed there are similar questions on stackoverflow but they all seem to have a solution related to the tech stack being used and I can't find anything related to this with sharp/node.
Perhaps the solution has to do with lossy encoding, I'm not sure, but my goal is to rotate images like the Photos app on windows which is able to rotate without increasing file size, or at least the original file size is restored when the image is rotated to its original orientation.
The code being used is pretty straightforward:
await sharp(filepath).rotate(90).toFile(output, rotateImgCallback);
Thank you @RobertKawecki for the insight that I needed. I tried grabbing the input images orientation with:
let imgPromise = sharp(filepath);
const { orientation } = await imgPromise.metadata();
then calculating the new orientation accordingly and setting it with:
await imgPromise.withMetadata({
orientation: newOrientation
}).toFile(output, rotateImgCallback);
without any rotate operation
This works great on jpeg but now I found out pngs don't use exif orientation so I have to figure something out for that as well.