@thi.ng/imago
Version:
JSON & API-based declarative and extensible image processing trees/pipelines
28 lines (27 loc) • 773 B
JavaScript
import { isNumber } from "@thi.ng/checks";
import { GRAVITY_POSITION } from "../api.js";
import { coerceColor, computeSize } from "../utils.js";
const resizeProc = async (spec, input, ctx) => {
const { bg, filter, fit, gravity, ref, size, unit } = spec;
const aspect = ctx.size[0] / ctx.size[1];
let $size = size;
let width, height;
if (isNumber($size) && unit !== "%") {
$size = aspect > 1 ? [$size, $size / aspect] : [$size * aspect, $size];
}
[width, height] = computeSize($size, ctx.size, ref, unit);
return [
input.resize({
width,
height,
fit,
kernel: filter,
position: gravity ? GRAVITY_POSITION[gravity] : void 0,
background: bg ? coerceColor(bg) : void 0
}),
true
];
};
export {
resizeProc
};