@@ -350,11 +350,11 @@ export class RawImage {
350
350
* @param {Object } options Additional options for resizing.
351
351
* @param {0|1|2|3|4|5|string } [options.resample] The resampling method to use.
352
352
* @returns {Promise<RawImage> } `this` to support chaining.
353
+ * @throws {Error } If the width or height is not a whole number.
353
354
*/
354
355
async resize ( width , height , {
355
356
resample = 2 ,
356
357
} = { } ) {
357
-
358
358
// Do nothing if the image already has the desired size
359
359
if ( this . width === width && this . height === height ) {
360
360
return this ;
@@ -363,18 +363,26 @@ export class RawImage {
363
363
// Ensure resample method is a string
364
364
let resampleMethod = RESAMPLING_MAPPING [ resample ] ?? resample ;
365
365
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
+
366
376
// Calculate width / height to maintain aspect ratio, in the event that
367
377
// the user passed a null value in.
368
378
// This allows users to pass in something like `resize(320, null)` to
369
379
// resize to 320 width, but maintain aspect ratio.
370
- const nullish_width = isNullishDimension ( width ) ;
371
- const nullish_height = isNullishDimension ( height ) ;
372
380
if ( nullish_width && nullish_height ) {
373
381
return this ;
374
382
} else if ( nullish_width ) {
375
- width = ( height / this . height ) * this . width ;
383
+ width = Math . round ( ( height / this . height ) * this . width ) ;
376
384
} else if ( nullish_height ) {
377
- height = ( width / this . width ) * this . height ;
385
+ height = Math . round ( ( width / this . width ) * this . height ) ;
378
386
}
379
387
380
388
if ( IS_BROWSER_OR_WEBWORKER ) {
@@ -699,7 +707,7 @@ export class RawImage {
699
707
/**
700
708
* Split this image into individual bands. This method returns an array of individual image bands from an image.
701
709
* 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
+ *
703
711
* Inspired by PIL's `Image.split()` [function](https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.split).
704
712
* @returns {RawImage[] } An array containing bands.
705
713
*/
0 commit comments