Files
3dtours/backend/utils/imageHelper.js
T

31 lines
1.1 KiB
JavaScript

const sharp = require('sharp');
/**
* Resizes an image to standard 8K resolution (8192x4096) with 2:1 ratio and saves as JPEG
* @param {string} inputPath - Path to the original uploaded file
* @param {string} outputPath - Path to save the processed 8K JPEG image
*/
const resizeTo8K = async (inputPath, outputPath) => {
try {
await sharp(inputPath)
.rotate() // Tự động xoay ảnh dựa trên EXIF orientation
.resize(8192, 4096, {
fit: 'fill' // Ensures the output is exactly 8192x4096
})
.jpeg({
quality: 85,
progressive: false, // Tắt progressive để header đơn giản hơn cho piexifjs
chromaSubsampling: '4:2:0'
})
// Loại bỏ .withMetadata() để Sharp tạo ra file JPEG sạch nhất.
// Điều này giúp piexifjs không bị lỗi "Given data is not jpeg".
.toFile(outputPath);
} catch (error) {
throw new Error(`Sharp image processing failed: ${error.message}`);
}
};
module.exports = {
resizeTo8K
};