Skip to content

fix: wasm-image-processor resize wrapper #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions packages/wasm-image-processor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ function resize(image: photon.PhotonImage, width: number, height: number, usePer
const widthPercent = usePercent ? width : (width * 100.0) / imageWidth;
const heightPercent = usePercent ? height : (height * 100.0) / imageHeight;

return photon.resize(image, (imageWidth * widthPercent) / 100, (imageHeight * heightPercent) / 100, 1);
const newWidth = (imageWidth * widthPercent) / 100;
const newHeight = (imageHeight * heightPercent) / 100;

return photon.resize(image, newWidth, newHeight, 1);
}

/**
Expand Down Expand Up @@ -105,7 +108,9 @@ async function loadImage(pathOrURL: string): Promise<WasmImage> {
* Get the width of the image.
* @returns {number} The width of the image in pixels.
*/
width: (): number => image.get_width(),
width: (): number => {
return image.get_width();
},
/**
* Get the height of the image.
* @returns {number} The height of the image in pixels.
Expand All @@ -120,9 +125,26 @@ async function loadImage(pathOrURL: string): Promise<WasmImage> {
*/
resize: (width: number, height: number, usePercent = true): WasmImage => {
const resizedImage = resize(image, width, height, usePercent);

return {
...wrapper,
image: resizedImage,
width: (): number => resizedImage.get_width(),
height: (): number => resizedImage.get_height(),
resize: (w: number, h: number, up = true): WasmImage => {
const newResizedImage = resize(resizedImage, w, h, up);
return {
image: newResizedImage,
width: (): number => newResizedImage.get_width(),
height: (): number => newResizedImage.get_height(),
resize: wrapper.resize,
getImageResponse: (format: SupportedImageFormat, quality = 100.0): Response =>
getImageResponse(newResizedImage, format, quality),
clean: () => clean(newResizedImage),
};
},
getImageResponse: (format: SupportedImageFormat, quality = 100.0): Response =>
getImageResponse(resizedImage, format, quality),
clean: () => clean(resizedImage),
};
},
/**
Expand Down