about summary refs log tree commit diff
path: root/src/lib/images.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/images.ts')
-rw-r--r--src/lib/images.ts16
1 files changed, 16 insertions, 0 deletions
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}
+}