From bccc8a64d06310f91b607f2c0e175c26d42ff1fb Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Tue, 17 Jan 2023 18:35:37 -0600 Subject: Give a more sensible default crop in the post image picker (related #39) --- src/lib/images.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/lib') diff --git a/src/lib/images.ts b/src/lib/images.ts index f56308c49..6f0b6a4fc 100644 --- a/src/lib/images.ts +++ b/src/lib/images.ts @@ -112,3 +112,19 @@ export async function compressIfNeeded( maxSize, }) } + +export interface Dim { + width: number + height: number +} +export function scaleDownDimensions(dim: Dim, max: Dim): Dim { + if (dim.width < max.width && dim.height < max.height) { + return dim + } + let wScale = dim.width > max.width ? max.width / dim.width : 1 + let hScale = dim.height > max.height ? max.height / dim.height : 1 + if (wScale < hScale) { + return {width: dim.width * wScale, height: dim.height * wScale} + } + return {width: dim.width * hScale, height: dim.height * hScale} +} -- cgit 1.4.1