diff --git a/packages/server/package.json b/packages/server/package.json index 96953583..8eaacdc4 100755 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -11,6 +11,7 @@ "@aws-sdk/client-s3": "^3.310.0", "@corenode/utils": "0.28.26", "@foxify/events": "^2.1.0", + "@jimp/plugin-scale": "^0.22.7", "@tensorflow/tfjs-node": "4.0.0", "aws-sdk": "^2.1355.0", "axios": "^1.2.5", diff --git a/packages/server/src/controllers/FilesController/index.js b/packages/server/src/controllers/FilesController/index.js index 5cf272d1..d8ec2106 100755 --- a/packages/server/src/controllers/FilesController/index.js +++ b/packages/server/src/controllers/FilesController/index.js @@ -44,10 +44,24 @@ async function processImage(file) { if (width > maximuns.imageResolution.width || height > maximuns.imageResolution.height) { await new Promise((resolve, reject) => { + // calculate max resolution respecting aspect ratio + const resizedResolution = { + width: maximuns.imageResolution.width, + height: maximuns.imageResolution.height, + } + + if (width > height) { + resizedResolution.height = Math.floor((height / width) * maximuns.imageResolution.width) + } + + if (height > width) { + resizedResolution.width = Math.floor((width / height) * maximuns.imageResolution.height) + } + Jimp.read(file.filepath) .then((image) => { image - .resize(maximuns.imageResolution.width, maximuns.imageResolution.height) + .resize(resizedResolution.width, resizedResolution.height) .quality(maximuns.imageQuality) .write(file.filepath, resolve) })