Compare commits
16 Commits
main
...
amy-image-
Author | SHA1 | Date |
---|---|---|
Amy | 01a3e0f5ad | |
Amy | 11c939db53 | |
Amy | dde42f2dc0 | |
Amy | c9a01b85ed | |
Amy | a6bc1ec256 | |
Amy | 1e49241a0d | |
Amy | ddc59a7103 | |
Amy | d7e82feed6 | |
Amy | 3867be2c75 | |
Amy | 7f2645b1ae | |
Amy | 84eba39f7a | |
Amy | 49c272efa3 | |
Amy | 6860089d66 | |
Amy | 6a3b151ed2 | |
Amy | 2a62f0b508 | |
Amy | 19c5aecb07 |
|
@ -16,14 +16,16 @@ import { default as getImageDimensions } from "image-size";
|
|||
const SOURCE_DIRECTORY = "images";
|
||||
const DESTINATION_DIRECTORY = path.join("public", "images");
|
||||
|
||||
// directory where Meet the Team headshots are stored, relative to the source directory
|
||||
// directories are relative to SOURCE_DIRECTORY
|
||||
const TEAM_IMAGES_DIRECTORY = path.join("team", "");
|
||||
const EVENTS_IMAGES_DIRECTORY = path.join("events", "");
|
||||
|
||||
const IMAGE_MINIMUM_SIZE = 512;
|
||||
|
||||
const GET_ENCODER_FROM_EXTENSION: { [imageExtension: string]: string } = {
|
||||
jpg: "mozjpeg",
|
||||
jpeg: "mozjpeg",
|
||||
JPG: "mozjpeg",
|
||||
png: "oxipng",
|
||||
};
|
||||
|
||||
|
@ -35,6 +37,8 @@ const ENCODER_OPTIONS: { [encoder: string]: Record<string, unknown> } = {
|
|||
void optimizeImages();
|
||||
|
||||
export async function optimizeImages() {
|
||||
const startTime = Date.now();
|
||||
|
||||
const imagePaths = await getFilePathsInDirectory(SOURCE_DIRECTORY);
|
||||
await fse.emptyDir(DESTINATION_DIRECTORY);
|
||||
|
||||
|
@ -42,8 +46,13 @@ export async function optimizeImages() {
|
|||
const numberOfWorkers = Math.min(cpus().length, 8);
|
||||
const imagePool = new ImagePool(numberOfWorkers);
|
||||
|
||||
const batchSize = 40;
|
||||
|
||||
for (let i = 0; i < imagePaths.length; i += batchSize) {
|
||||
await Promise.all(
|
||||
imagePaths.map(async (imagePath) => {
|
||||
imagePaths.slice(i, i + batchSize).map(async (imagePath) => {
|
||||
const imageStartTime = Date.now();
|
||||
|
||||
const sourcePath = path.join(SOURCE_DIRECTORY, imagePath);
|
||||
const destinationPath = path.join(DESTINATION_DIRECTORY, imagePath);
|
||||
const fileExtension = imagePath.split(".").pop() ?? "";
|
||||
|
@ -51,6 +60,9 @@ export async function optimizeImages() {
|
|||
|
||||
if (!encoder) {
|
||||
await fse.copy(sourcePath, destinationPath);
|
||||
console.log(
|
||||
`Copied ${imagePath} in ${getElapsedSeconds(imageStartTime)}s`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -61,7 +73,8 @@ export async function optimizeImages() {
|
|||
await ingestedImage.decoded;
|
||||
|
||||
const shouldResize =
|
||||
imagePath.startsWith(TEAM_IMAGES_DIRECTORY) &&
|
||||
(imagePath.startsWith(TEAM_IMAGES_DIRECTORY) ||
|
||||
imagePath.startsWith(EVENTS_IMAGES_DIRECTORY)) &&
|
||||
(width ?? 0) > IMAGE_MINIMUM_SIZE &&
|
||||
(height ?? 0) > IMAGE_MINIMUM_SIZE;
|
||||
|
||||
|
@ -77,6 +90,10 @@ export async function optimizeImages() {
|
|||
};
|
||||
|
||||
await ingestedImage.preprocess(preprocessOptions);
|
||||
|
||||
console.log(
|
||||
`Resized ${sourcePath} in ${getElapsedSeconds(imageStartTime)}s`
|
||||
);
|
||||
}
|
||||
|
||||
const encodeOptions = { [encoder]: ENCODER_OPTIONS[encoder] };
|
||||
|
@ -84,10 +101,17 @@ export async function optimizeImages() {
|
|||
|
||||
const encodedImage = await ingestedImage.encodedWith[encoder];
|
||||
await fse.outputFile(destinationPath, encodedImage.binary);
|
||||
|
||||
console.log(
|
||||
`Optimized ${sourcePath} in ${getElapsedSeconds(imageStartTime)}s`
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
await imagePool.close();
|
||||
|
||||
console.log(`TOTAL DURATION: ${getElapsedSeconds(startTime)}s`);
|
||||
}
|
||||
|
||||
async function getFilePathsInDirectory(directory: string): Promise<string[]> {
|
||||
|
@ -105,3 +129,7 @@ async function getFilePathsInDirectory(directory: string): Promise<string[]> {
|
|||
)
|
||||
).flat();
|
||||
}
|
||||
|
||||
function getElapsedSeconds(startTime: number) {
|
||||
return (Date.now() - startTime) / 1000;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue