Skip to content

Commit 633764f

Browse files
Fix that resize could be an none whole number causing issues.
1 parent e1753ac commit 633764f

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/utils/image.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,11 @@ export class RawImage {
350350
* @param {Object} options Additional options for resizing.
351351
* @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use.
352352
* @returns {Promise<RawImage>} `this` to support chaining.
353+
* @throws {Error} If the width or height is not a whole number.
353354
*/
354355
async resize(width, height, {
355356
resample = 2,
356357
} = {}) {
357-
358358
// Do nothing if the image already has the desired size
359359
if (this.width === width && this.height === height) {
360360
return this;
@@ -363,18 +363,26 @@ export class RawImage {
363363
// Ensure resample method is a string
364364
let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample;
365365

366+
const nullish_width = isNullishDimension(width);
367+
const nullish_height = isNullishDimension(height);
368+
// Width and height must be whole numbers.
369+
if (!(Number.isInteger(width) || nullish_width)) {
370+
throw new Error(`Width must be an integer, but got ${width}`);
371+
}
372+
if (!(Number.isInteger(height) || nullish_height)) {
373+
throw new Error(`Height must be an integer, but got ${height}`);
374+
}
375+
366376
// Calculate width / height to maintain aspect ratio, in the event that
367377
// the user passed a null value in.
368378
// This allows users to pass in something like `resize(320, null)` to
369379
// resize to 320 width, but maintain aspect ratio.
370-
const nullish_width = isNullishDimension(width);
371-
const nullish_height = isNullishDimension(height);
372380
if (nullish_width && nullish_height) {
373381
return this;
374382
} else if (nullish_width) {
375-
width = (height / this.height) * this.width;
383+
width = Math.round((height / this.height) * this.width);
376384
} else if (nullish_height) {
377-
height = (width / this.width) * this.height;
385+
height = Math.round((width / this.width) * this.height);
378386
}
379387

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

0 commit comments

Comments
 (0)