Ensure invalid resize width/height as options throw #1817

This commit is contained in:
Lovell Fuller 2019-08-16 21:21:12 +01:00
parent da4e05c118
commit 69fe21a7ec
3 changed files with 27 additions and 4 deletions

View File

@ -9,6 +9,9 @@ Requires libvips v8.8.1.
* Ensure `sharp.format.vips` is present and correct (filesystem only).
[#1813](https://github.com/lovell/sharp/issues/1813)
* Ensure invalid `width` and `height` provided as options to `resize` throw.
[#1817](https://github.com/lovell/sharp/issues/1817)
* Allow use of 'heic' and 'heif' identifiers with `toFormat`.
[#1834](https://github.com/lovell/sharp/pull/1834)
[@jaubourg](https://github.com/jaubourg)

View File

@ -210,12 +210,20 @@ function resize (width, height, options) {
}
if (is.object(options)) {
// Width
if (is.defined(options.width)) {
if (is.integer(options.width) && options.width > 0) {
this.options.width = options.width;
} else {
throw is.invalidParameterError('width', 'positive integer', options.width);
}
}
// Height
if (is.defined(options.height)) {
if (is.integer(options.height) && options.height > 0) {
this.options.height = options.height;
} else {
throw is.invalidParameterError('height', 'positive integer', options.height);
}
}
// Fit
if (is.defined(options.fit)) {

View File

@ -87,6 +87,18 @@ describe('Resize dimensions', function () {
}, /Expected positive integer for height but received 1.5 of type number/);
});
it('Invalid width - via options', () => {
assert.throws(() => {
sharp().resize({ width: 1.5, height: 240 });
}, /Expected positive integer for width but received 1.5 of type number/);
});
it('Invalid height - via options', () => {
assert.throws(() => {
sharp().resize({ width: 320, height: 1.5 });
}, /Expected positive integer for height but received 1.5 of type number/);
});
it('Invalid width - too large', function (done) {
sharp(fixtures.inputJpg)
.resize(0x4000, 1)