34 lines
1.5 KiB
JavaScript
34 lines
1.5 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 {
|
|
// Sử dụng failOn: 'none' để Sharp không dừng lại khi gặp lỗi metadata nhỏ trong tệp RAW/DNG.
|
|
// Khi libvips trên server hỗ trợ libraw, Sharp sẽ tự động render dữ liệu DNG này.
|
|
await sharp(inputPath, { failOn: 'none' })
|
|
.removeAlpha() // Loại bỏ các kênh phụ/alpha thường gây lỗi 'multiband' trên tệp DNG
|
|
.rotate() // Tự động xoay ảnh dựa trên EXIF orientation
|
|
.resize(8192, 4096, {
|
|
fit: 'cover' // Thay đổi thành 'cover' để giữ tỷ lệ 2:1 mà không làm méo ảnh
|
|
})
|
|
.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
|
|
};
|