u
This commit is contained in:
33
server.js
33
server.js
@@ -148,24 +148,41 @@ app.post('/upload', authenticate, upload.single('image'), async (req, res) => {
|
|||||||
|
|
||||||
// Check if we need to modify the image
|
// Check if we need to modify the image
|
||||||
const shouldRotate = settings.rotation && settings.rotation !== 0;
|
const shouldRotate = settings.rotation && settings.rotation !== 0;
|
||||||
const shouldCrop = settings.crop && settings.crop.width > 0 && settings.crop.height > 0;
|
// Crop if width/height > 0, OR if left/top > 0 (then we calculate width/height)
|
||||||
|
const hasCropOffset = settings.crop && (settings.crop.left > 0 || settings.crop.top > 0);
|
||||||
|
const hasCropSize = settings.crop && settings.crop.width > 0 && settings.crop.height > 0;
|
||||||
|
const shouldCrop = hasCropOffset || hasCropSize;
|
||||||
|
|
||||||
if (shouldRotate || shouldCrop) {
|
if (shouldRotate || shouldCrop) {
|
||||||
console.log(`Applying transformations for ${cameraId}:`, { rotation: settings.rotation, crop: settings.crop });
|
console.log(`Applying transformations for ${cameraId}:`, { rotation: settings.rotation, crop: settings.crop });
|
||||||
let pipeline = sharp(req.file.path);
|
|
||||||
|
|
||||||
|
let imageBuffer = fs.readFileSync(req.file.path);
|
||||||
|
|
||||||
|
// Apply rotation first if needed
|
||||||
if (shouldRotate) {
|
if (shouldRotate) {
|
||||||
pipeline = pipeline.rotate(settings.rotation);
|
imageBuffer = await sharp(imageBuffer).rotate(settings.rotation).toBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply crop on the (potentially rotated) image
|
||||||
if (shouldCrop) {
|
if (shouldCrop) {
|
||||||
// crop format: { left, top, width, height }
|
const metadata = await sharp(imageBuffer).metadata();
|
||||||
pipeline = pipeline.extract(settings.crop);
|
// Calculate crop params, ensure at least 1px dimensions
|
||||||
|
const cropParams = {
|
||||||
|
left: Math.min(settings.crop.left || 0, metadata.width - 1),
|
||||||
|
top: Math.min(settings.crop.top || 0, metadata.height - 1),
|
||||||
|
width: settings.crop.width > 0
|
||||||
|
? settings.crop.width
|
||||||
|
: Math.max(1, metadata.width - (settings.crop.left || 0)),
|
||||||
|
height: settings.crop.height > 0
|
||||||
|
? settings.crop.height
|
||||||
|
: Math.max(1, metadata.height - (settings.crop.top || 0))
|
||||||
|
};
|
||||||
|
console.log(`Calculated crop params:`, cropParams);
|
||||||
|
imageBuffer = await sharp(imageBuffer).extract(cropParams).toBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overwrite the original file with transformed version
|
// Write final result
|
||||||
const buffer = await pipeline.toBuffer();
|
fs.writeFileSync(req.file.path, imageBuffer);
|
||||||
fs.writeFileSync(req.file.path, buffer);
|
|
||||||
console.log(`Transformed image saved to ${req.file.path}`);
|
console.log(`Transformed image saved to ${req.file.path}`);
|
||||||
}
|
}
|
||||||
} catch (transformError) {
|
} catch (transformError) {
|
||||||
|
|||||||
Reference in New Issue
Block a user