Skip to content

Commit

Permalink
Fix that resize could be an none whole number causing issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
BritishWerewolf committed Dec 16, 2024
1 parent 610391d commit d4cfacb
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/utils/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ export class RawImage {
* @param {Object} options Additional options for resizing.
* @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use.
* @returns {Promise<RawImage>} `this` to support chaining.
* @throws {Error} If the width or height is not a whole number.
*/
async resize(width, height, {
resample = 2,
} = {}) {

// Do nothing if the image already has the desired size
if (this.width === width && this.height === height) {
return this;
Expand All @@ -363,18 +363,26 @@ export class RawImage {
// Ensure resample method is a string
let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample;

const nullish_width = isNullishDimension(width);
const nullish_height = isNullishDimension(height);
// Width and height must be whole numbers.
if (!(Number.isInteger(width) || nullish_width)) {
throw new Error(`Width must be an integer, but got ${width}`);
}
if (!(Number.isInteger(height) || nullish_height)) {
throw new Error(`Height must be an integer, but got ${height}`);
}

// Calculate width / height to maintain aspect ratio, in the event that
// the user passed a null value in.
// This allows users to pass in something like `resize(320, null)` to
// resize to 320 width, but maintain aspect ratio.
const nullish_width = isNullishDimension(width);
const nullish_height = isNullishDimension(height);
if (nullish_width && nullish_height) {
return this;
} else if (nullish_width) {
width = (height / this.height) * this.width;
width = Math.round((height / this.height) * this.width);
} else if (nullish_height) {
height = (width / this.width) * this.height;
height = Math.round((width / this.width) * this.height);
}

if (IS_BROWSER_OR_WEBWORKER) {
Expand Down Expand Up @@ -699,7 +707,7 @@ export class RawImage {
/**
* Split this image into individual bands. This method returns an array of individual image bands from an image.
* For example, splitting an "RGB" image creates three new images each containing a copy of one of the original bands (red, green, blue).
*
*
* Inspired by PIL's `Image.split()` [function](https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.split).
* @returns {RawImage[]} An array containing bands.
*/
Expand Down

0 comments on commit d4cfacb

Please sign in to comment.