use aspect ratio to rescale

This commit is contained in:
SrGooglo 2023-04-13 00:55:38 +00:00
parent bb3b8a150f
commit 195c585049
2 changed files with 16 additions and 1 deletions

View File

@ -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",

View File

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