keep aspect ratio on previews

This commit is contained in:
2020-10-16 15:57:08 +00:00
committed by Stepan Usatiuk
parent 537587b052
commit 5d079a40af

View File

@@ -39,7 +39,24 @@ export async function resizeTo(
outPath: string,
size: number,
): Promise<void> {
await sharp(inPath).resize(size, size).withMetadata().toFile(outPath);
const file = sharp(inPath);
const metadata = await file.metadata();
if (!(metadata.width && metadata.height)) {
throw new Error(
`The ${inPath} doesn't have width and height... how did we get there?`,
);
}
const wider = metadata.width > metadata.height;
const ratio = wider
? metadata.height / metadata.width
: metadata.width / metadata.height;
const newWidth = Math.floor(wider ? size : size * ratio);
const newHeight = Math.floor(wider ? size * ratio : size);
await sharp(inPath)
.resize(newWidth, newHeight)
.withMetadata()
.toFile(outPath);
}
// eslint-disable-next-line @typescript-eslint/no-misused-promises