2023-10-14 10:18:43 +02:00
|
|
|
import fs from "fs";
|
2023-11-26 14:06:23 +01:00
|
|
|
import sharp, { FormatEnum, Metadata } from "sharp";
|
2023-09-10 11:14:04 +02:00
|
|
|
import logit from "./logit";
|
2024-02-08 15:57:35 +01:00
|
|
|
import { compression, customWidth } from "./config-variables";
|
2024-01-15 10:07:22 +01:00
|
|
|
import { ImageFormat } from "./types";
|
2023-09-10 11:14:04 +02:00
|
|
|
|
|
|
|
const convertAndScale = async (
|
|
|
|
originalImagePath: string,
|
|
|
|
upscaledImagePath: string,
|
|
|
|
processedImagePath: string,
|
|
|
|
scale: string,
|
2024-01-15 10:07:22 +01:00
|
|
|
saveImageAs: ImageFormat,
|
2024-02-10 12:25:07 +01:00
|
|
|
imageIsAlpha?: boolean,
|
2023-09-10 11:14:04 +02:00
|
|
|
) => {
|
2024-02-10 12:25:07 +01:00
|
|
|
// Skip conversion when the scale is 4x and compression is 0
|
|
|
|
// - When output format is WebP or PNG
|
|
|
|
// - When the image is not alpha and the output format is JPEG
|
|
|
|
if (
|
|
|
|
!customWidth &&
|
|
|
|
(saveImageAs === "png" || saveImageAs === "webp") &&
|
|
|
|
scale === "4" &&
|
|
|
|
compression === 0
|
|
|
|
) {
|
|
|
|
logit("Skipping compression for 4x scale and 0% compression");
|
|
|
|
return;
|
|
|
|
} else if (
|
|
|
|
!customWidth &&
|
|
|
|
saveImageAs === "jpg" &&
|
|
|
|
scale === "4" &&
|
|
|
|
compression === 0 &&
|
|
|
|
!imageIsAlpha
|
|
|
|
) {
|
2024-01-23 10:30:08 +01:00
|
|
|
logit("Skipping compression for 4x scale and 0% compression");
|
2023-10-14 10:48:14 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-02-10 12:25:07 +01:00
|
|
|
|
2023-11-26 14:06:23 +01:00
|
|
|
let originalImage: Metadata | undefined;
|
2023-09-10 19:42:18 +02:00
|
|
|
|
2023-11-26 14:06:23 +01:00
|
|
|
try {
|
|
|
|
originalImage = await sharp(originalImagePath).metadata();
|
|
|
|
} catch (error) {
|
2024-01-15 12:30:59 +01:00
|
|
|
logit("❌ Error with original Image: ", error, " - ", originalImagePath);
|
2023-11-26 14:06:23 +01:00
|
|
|
}
|
2023-09-17 01:59:18 +02:00
|
|
|
|
2023-10-14 10:18:43 +02:00
|
|
|
fs.access(originalImagePath, fs.constants.F_OK, (err) => {
|
2024-01-15 12:30:59 +01:00
|
|
|
logit("🖼️ Checking if original image exists: ", originalImagePath);
|
2023-10-14 10:18:43 +02:00
|
|
|
if (err) {
|
2024-01-15 12:30:59 +01:00
|
|
|
throw new Error(
|
2024-02-08 15:57:35 +01:00
|
|
|
"Could not grab the original image from the path provided! - " + err,
|
2024-01-15 12:30:59 +01:00
|
|
|
);
|
2023-10-14 10:18:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-15 12:30:59 +01:00
|
|
|
if (!originalImage) {
|
2023-09-10 11:14:04 +02:00
|
|
|
throw new Error("Could not grab the original image!");
|
|
|
|
}
|
2023-09-18 20:30:43 +02:00
|
|
|
|
2023-09-16 12:34:35 +02:00
|
|
|
// Convert compression percentage (0-100) to compressionLevel (0-9)
|
|
|
|
const compressionLevel = Math.round((compression / 100) * 9);
|
2023-09-18 20:30:43 +02:00
|
|
|
|
2023-10-14 10:41:57 +02:00
|
|
|
logit(
|
|
|
|
"📐 Processing Image: ",
|
|
|
|
JSON.stringify({
|
|
|
|
originalWidth: originalImage.width,
|
|
|
|
originalHeight: originalImage.height,
|
2024-02-08 15:57:35 +01:00
|
|
|
customWidth,
|
2023-10-14 10:41:57 +02:00
|
|
|
scale,
|
|
|
|
saveImageAs,
|
|
|
|
compressionPercentage: compression,
|
|
|
|
compressionLevel,
|
2024-02-08 15:57:35 +01:00
|
|
|
}),
|
2023-10-14 10:41:57 +02:00
|
|
|
);
|
2023-09-17 01:59:18 +02:00
|
|
|
|
2024-02-08 15:57:35 +01:00
|
|
|
// Resize the image to the scale
|
|
|
|
const newImage = sharp(upscaledImagePath, {
|
|
|
|
limitInputPixels: false,
|
|
|
|
})
|
|
|
|
.resize(
|
|
|
|
customWidth
|
|
|
|
? parseInt(customWidth)
|
|
|
|
: originalImage.width && originalImage.width * parseInt(scale),
|
|
|
|
customWidth
|
|
|
|
? null
|
|
|
|
: originalImage.height && originalImage.height * parseInt(scale),
|
|
|
|
)
|
|
|
|
.withMetadata({
|
|
|
|
density: originalImage.density,
|
|
|
|
orientation: originalImage.orientation,
|
|
|
|
});
|
|
|
|
|
2024-01-21 07:48:22 +01:00
|
|
|
const buffer = await newImage
|
|
|
|
.withMetadata({
|
|
|
|
density: originalImage.density,
|
|
|
|
orientation: originalImage.orientation,
|
|
|
|
})
|
|
|
|
.toBuffer();
|
|
|
|
|
2023-09-13 18:15:42 +02:00
|
|
|
try {
|
2023-09-16 12:34:35 +02:00
|
|
|
await sharp(buffer, {
|
|
|
|
limitInputPixels: false,
|
2023-09-17 01:59:18 +02:00
|
|
|
})
|
2024-01-21 07:48:22 +01:00
|
|
|
.withMetadata({
|
|
|
|
density: originalImage.density,
|
|
|
|
orientation: originalImage.orientation,
|
|
|
|
})
|
2023-09-17 02:14:41 +02:00
|
|
|
.toFormat(saveImageAs as keyof FormatEnum, {
|
2023-09-17 13:37:57 +02:00
|
|
|
...(saveImageAs === "jpg" && {
|
|
|
|
quality: 100 - (compression === 100 ? 99 : compression),
|
|
|
|
chromaSubsampling: "4:4:4",
|
|
|
|
}),
|
2023-11-23 06:39:46 +01:00
|
|
|
// For PNGs, compression enables indexed colors automatically,
|
|
|
|
// so we need to warn the user that this will happen
|
|
|
|
// https://sharp.pixelplumbing.com/api-output#png
|
|
|
|
...(saveImageAs === "png" &&
|
|
|
|
compression > 0 && {
|
|
|
|
...(compression > 0 && {
|
|
|
|
quality: 100 - (compression === 100 ? 99 : compression),
|
|
|
|
}),
|
|
|
|
compressionLevel: 9,
|
|
|
|
}),
|
2024-01-15 10:25:29 +01:00
|
|
|
...(saveImageAs === "webp" && {
|
|
|
|
quality: 100 - (compression === 100 ? 99 : compression),
|
|
|
|
alphaQuality: 100,
|
|
|
|
lossless: compression === 0,
|
|
|
|
smartSubsample: true,
|
|
|
|
}),
|
2023-09-17 13:37:57 +02:00
|
|
|
force: true,
|
2023-09-17 02:14:41 +02:00
|
|
|
})
|
2023-09-17 01:59:18 +02:00
|
|
|
.toFile(processedImagePath);
|
2023-09-13 18:15:42 +02:00
|
|
|
} catch (error) {
|
|
|
|
logit("❌ Error converting to: ", saveImageAs, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
logit("✅ Done converting to: ", upscaledImagePath);
|
2023-09-10 11:14:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default convertAndScale;
|