Add encoder for PNGs
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Amy 2021-10-24 01:23:06 -04:00
parent 92aab308b0
commit bcf9ff2c73
2 changed files with 13 additions and 7 deletions

View File

@ -52,6 +52,7 @@ export async function getMemberImagePath(name: string) {
(await getImage(imgPath + ".jpg")) ??
(await getImage(imgPath + ".png")) ??
(await getImage(imgPath + ".gif")) ??
(await getImage(imgPath + ".jpeg")) ??
placeholder;
return img;
}

View File

@ -20,10 +20,15 @@ const TEAM_IMAGES_DIRECTORY = path.join("team", "");
const IMAGE_MINIMUM_SIZE = 512;
const IMAGE_ENCODE_OPTIONS = { mozjpeg: {} };
const GET_CODEC_FROM_EXTENSION: { [imageExtension: string]: string } = {
const GET_ENCODER_FROM_EXTENSION: { [imageExtension: string]: string } = {
jpg: "mozjpeg",
jpeg: "mozjpeg",
png: "oxipng",
};
const ENCODER_OPTIONS: { [encoder: string]: Record<string, unknown> } = {
mozjpeg: {},
oxipng: {},
};
void optimizeImages();
@ -40,8 +45,9 @@ export async function optimizeImages() {
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];
if (!GET_CODEC_FROM_EXTENSION[fileExtension]) {
if (!encoder) {
await fse.copy(sourcePath, destinationPath);
return;
}
@ -71,11 +77,10 @@ export async function optimizeImages() {
await ingestedImage.preprocess(preprocessOptions);
}
await ingestedImage.encode(IMAGE_ENCODE_OPTIONS);
const encodeOptions = { [encoder]: ENCODER_OPTIONS[encoder] };
await ingestedImage.encode(encodeOptions);
const encodedImage = await ingestedImage.encodedWith[
GET_CODEC_FROM_EXTENSION[fileExtension]
];
const encodedImage = await ingestedImage.encodedWith[encoder];
await fse.outputFile(destinationPath, encodedImage.binary);
})
);