Fixed cropping regression

This commit is contained in:
Roman Krček
2025-07-18 09:33:21 +02:00
parent 2e228126be
commit ceececfd99

View File

@@ -228,18 +228,31 @@
// Compute crop size and center
let cropWidth = faceWidth * cropScale;
let cropHeight = cropWidth / cropRatio;
// If crop is larger than image, scale it down while maintaining aspect ratio
if (cropWidth > img.naturalWidth || cropHeight > img.naturalHeight) {
const widthRatio = img.naturalWidth / cropWidth;
const heightRatio = img.naturalHeight / cropHeight;
const scale = Math.min(widthRatio, heightRatio);
cropWidth *= scale;
cropHeight *= scale;
}
let centerX = faceCenterX + cropWidth * offsetX;
let centerY = faceCenterY + cropHeight * offsetY;
// Clamp center to ensure crop fits
centerX = Math.max(cropWidth/2, Math.min(centerX, img.naturalWidth - cropWidth/2));
centerY = Math.max(cropHeight/2, Math.min(centerY, img.naturalHeight - cropHeight/2));
const cropX = Math.round(centerX - cropWidth/2);
const cropY = Math.round(centerY - cropHeight/2);
const crop = {
x: Math.max(0, Math.min(cropX, img.naturalWidth - cropWidth)),
y: Math.max(0, Math.min(cropY, img.naturalHeight - cropHeight)),
width: Math.round(Math.min(cropWidth, img.naturalWidth)),
height: Math.round(Math.min(cropHeight, img.naturalHeight))
x: Math.max(0, cropX),
y: Math.max(0, cropY),
width: Math.round(cropWidth),
height: Math.round(cropHeight)
};
photos[index].cropData = crop;
photos[index].faceDetectionStatus = 'completed';