Skip to content

Commit 6b13b0d

Browse files
feat(imago): update resize, add support for proportional resize
- update resizeProc() to handle scalar `size` to scale proportionally with automatic aspect detection
1 parent 25d8377 commit 6b13b0d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

packages/imago/src/ops/resize.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import { isNumber } from "@thi.ng/checks";
12
import { GRAVITY_POSITION, type Processor, type ResizeSpec } from "../api.js";
23
import { coerceColor, computeSize } from "../units.js";
34

45
export const resizeProc: Processor = async (spec, input, ctx) => {
56
const { bg, filter, fit, gravity, ref, size, unit } = <ResizeSpec>spec;
6-
const [width, height] = computeSize(size, ctx.size, ref, unit);
7+
const aspect = ctx.size[0] / ctx.size[1];
8+
let $size = size;
9+
let width: number, height: number;
10+
if (isNumber($size) && unit !== "%") {
11+
$size = aspect > 1 ? [$size, $size / aspect] : [$size * aspect, $size];
12+
}
13+
[width, height] = computeSize($size, ctx.size, ref, unit);
714
return [
815
input.resize({
916
width,

0 commit comments

Comments
 (0)