From 75d571cb6c9d35dcddfc89e87935b95d01ccbc34 Mon Sep 17 00:00:00 2001 From: Surma Date: Mon, 2 Nov 2020 15:14:07 +0000 Subject: [PATCH] Add proper aspect ratio resizing support --- cli/src/codecs.js | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/cli/src/codecs.js b/cli/src/codecs.js index f2317ddb..9c93dce8 100644 --- a/cli/src/codecs.js +++ b/cli/src/codecs.js @@ -53,6 +53,32 @@ function resizeNameToIndex(name) { } } +function resizeWithAspect({ + input_width, + input_height, + target_width, + target_height +}) { + if (!target_width && !target_height) { + throw Error("Need to specify at least width or height when resizing"); + } + if (target_width && target_height) { + return { width: target_width, height: target_height }; + } + if (!target_width) { + return { + width: Math.round((input_width / input_height) * target_height), + height: target_height + }; + } + if (!target_height) { + return { + width: target_width, + height: Math.round((input_height / input_width) * target_width) + }; + } +} + export const preprocessors = { resize: { name: "Resize", @@ -64,8 +90,14 @@ export const preprocessors = { input_width, input_height, { width, height, method, premultiply, linearRGB } - ) => - new ImageData( + ) => { + ({ width, height } = resizeWithAspect({ + input_width, + input_height, + target_width: width, + target_height: height + })); + return new ImageData( resize.resize( buffer, input_width, @@ -79,12 +111,9 @@ export const preprocessors = { width, height ); + }; }, defaultOptions: { - // Width and height will always default to the image size. - // This is set elsewhere. - width: 1, - height: 1, // This will be set to 'vector' if the input is SVG. method: "lanczos3", fitMethod: "stretch",