mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-14 17:49:52 +00:00
Add proper aspect ratio resizing support
This commit is contained in:
@@ -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 = {
|
export const preprocessors = {
|
||||||
resize: {
|
resize: {
|
||||||
name: "Resize",
|
name: "Resize",
|
||||||
@@ -64,8 +90,14 @@ export const preprocessors = {
|
|||||||
input_width,
|
input_width,
|
||||||
input_height,
|
input_height,
|
||||||
{ width, height, method, premultiply, linearRGB }
|
{ 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(
|
resize.resize(
|
||||||
buffer,
|
buffer,
|
||||||
input_width,
|
input_width,
|
||||||
@@ -79,12 +111,9 @@ export const preprocessors = {
|
|||||||
width,
|
width,
|
||||||
height
|
height
|
||||||
);
|
);
|
||||||
|
};
|
||||||
},
|
},
|
||||||
defaultOptions: {
|
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.
|
// This will be set to 'vector' if the input is SVG.
|
||||||
method: "lanczos3",
|
method: "lanczos3",
|
||||||
fitMethod: "stretch",
|
fitMethod: "stretch",
|
||||||
|
|||||||
Reference in New Issue
Block a user