Compare commits

...

16 Commits

Author SHA1 Message Date
Amy 01a3e0f5ad Try batches of 40
continuous-integration/drone/push Build is passing Details
2022-06-04 18:42:27 -04:00
Amy 11c939db53 Try batches of 48
continuous-integration/drone/push Build is failing Details
2022-06-04 18:36:48 -04:00
Amy dde42f2dc0 Try batches of 64
continuous-integration/drone/push Build is failing Details
2022-06-04 18:30:17 -04:00
Amy c9a01b85ed Try batches of 32
continuous-integration/drone/push Build is passing Details
2022-06-04 18:19:13 -04:00
Amy a6bc1ec256 Try Promise.all on batches of 16
continuous-integration/drone/push Build is passing Details
2022-06-04 18:07:33 -04:00
Amy 1e49241a0d Revert "Try for loop instead of Promise.all"
This reverts commit ddc59a7103.
2022-06-04 18:02:53 -04:00
Amy ddc59a7103 Try for loop instead of Promise.all
continuous-integration/drone/push Build is passing Details
2022-06-04 17:57:43 -04:00
Amy d7e82feed6 Revert "Try 4 workers"
This reverts commit 3867be2c75.
2022-06-04 17:55:07 -04:00
Amy 3867be2c75 Try 4 workers
continuous-integration/drone/push Build is failing Details
2022-06-04 17:23:44 -04:00
Amy 7f2645b1ae Revert "Revert "Revert "Replace events images with optimized images"""
continuous-integration/drone/push Build is failing Details
This reverts commit 84eba39f7a.
2022-06-04 17:18:10 -04:00
Amy 84eba39f7a Revert "Revert "Replace events images with optimized images""
continuous-integration/drone/push Build is passing Details
This reverts commit 6860089d66.
2022-06-04 16:48:11 -04:00
Amy 49c272efa3 Fix brackets
continuous-integration/drone/push Build is failing Details
2022-06-04 16:21:33 -04:00
Amy 6860089d66 Revert "Replace events images with optimized images"
This reverts commit 6a3b151ed2.
2022-06-04 16:19:50 -04:00
Amy 6a3b151ed2 Replace events images with optimized images
continuous-integration/drone/push Build is passing Details
2022-06-04 15:52:05 -04:00
Amy 2a62f0b508 Add resize logging
continuous-integration/drone/push Build is failing Details
2022-06-04 15:19:27 -04:00
Amy 19c5aecb07 Resize events images and add logging
continuous-integration/drone/push Build is failing Details
2022-06-04 14:51:40 -04:00
1 changed files with 64 additions and 36 deletions

View File

@ -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,52 +46,72 @@ export async function optimizeImages() {
const numberOfWorkers = Math.min(cpus().length, 8);
const imagePool = new ImagePool(numberOfWorkers);
await Promise.all(
imagePaths.map(async (imagePath) => {
const sourcePath = path.join(SOURCE_DIRECTORY, imagePath);
const destinationPath = path.join(DESTINATION_DIRECTORY, imagePath);
const fileExtension = imagePath.split(".").pop() ?? "";
const encoder = GET_ENCODER_FROM_EXTENSION[fileExtension];
const batchSize = 40;
if (!encoder) {
await fse.copy(sourcePath, destinationPath);
return;
}
for (let i = 0; i < imagePaths.length; i += batchSize) {
await Promise.all(
imagePaths.slice(i, i + batchSize).map(async (imagePath) => {
const imageStartTime = Date.now();
const rawImageFile = await fse.readFile(sourcePath);
const ingestedImage = imagePool.ingestImage(rawImageFile);
const { width, height } = getImageDimensions(rawImageFile);
const sourcePath = path.join(SOURCE_DIRECTORY, imagePath);
const destinationPath = path.join(DESTINATION_DIRECTORY, imagePath);
const fileExtension = imagePath.split(".").pop() ?? "";
const encoder = GET_ENCODER_FROM_EXTENSION[fileExtension];
await ingestedImage.decoded;
if (!encoder) {
await fse.copy(sourcePath, destinationPath);
console.log(
`Copied ${imagePath} in ${getElapsedSeconds(imageStartTime)}s`
);
return;
}
const shouldResize =
imagePath.startsWith(TEAM_IMAGES_DIRECTORY) &&
(width ?? 0) > IMAGE_MINIMUM_SIZE &&
(height ?? 0) > IMAGE_MINIMUM_SIZE;
const rawImageFile = await fse.readFile(sourcePath);
const ingestedImage = imagePool.ingestImage(rawImageFile);
const { width, height } = getImageDimensions(rawImageFile);
if (width && height && shouldResize) {
const smallerDimension = width < height ? "width" : "height";
await ingestedImage.decoded;
// specifying only one dimension maintains the aspect ratio
const preprocessOptions = {
resize: {
enabled: true,
[smallerDimension]: IMAGE_MINIMUM_SIZE,
},
};
const shouldResize =
(imagePath.startsWith(TEAM_IMAGES_DIRECTORY) ||
imagePath.startsWith(EVENTS_IMAGES_DIRECTORY)) &&
(width ?? 0) > IMAGE_MINIMUM_SIZE &&
(height ?? 0) > IMAGE_MINIMUM_SIZE;
await ingestedImage.preprocess(preprocessOptions);
}
if (width && height && shouldResize) {
const smallerDimension = width < height ? "width" : "height";
const encodeOptions = { [encoder]: ENCODER_OPTIONS[encoder] };
await ingestedImage.encode(encodeOptions);
// specifying only one dimension maintains the aspect ratio
const preprocessOptions = {
resize: {
enabled: true,
[smallerDimension]: IMAGE_MINIMUM_SIZE,
},
};
const encodedImage = await ingestedImage.encodedWith[encoder];
await fse.outputFile(destinationPath, encodedImage.binary);
})
);
await ingestedImage.preprocess(preprocessOptions);
console.log(
`Resized ${sourcePath} in ${getElapsedSeconds(imageStartTime)}s`
);
}
const encodeOptions = { [encoder]: ENCODER_OPTIONS[encoder] };
await ingestedImage.encode(encodeOptions);
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;
}