Keep output dimensions within WebP 14-bit range

This commit is contained in:
Lovell Fuller 2014-12-06 22:33:47 +00:00
parent 065ce6454b
commit 464fb1726d
4 changed files with 52 additions and 8 deletions

View File

@ -110,7 +110,7 @@ readableStream.pipe(transformer).pipe(writableStream);
```javascript ```javascript
var image = sharp(inputJpg); var image = sharp(inputJpg);
image.metadata(function(err, metadata) { image.metadata(function(err, metadata) {
image.resize(metadata.width / 2).webp().toBuffer(function(err, outputBuffer, info) { image.resize(Math.floor(metadata.width / 2)).webp().toBuffer(function(err, outputBuffer, info) {
// outputBuffer contains a WebP image half the width and height of the original JPEG // outputBuffer contains a WebP image half the width and height of the original JPEG
}); });
}); });
@ -259,9 +259,9 @@ An advanced setting that switches the libvips access method to `VIPS_ACCESS_SEQU
Scale output to `width` x `height`. By default, the resized image is cropped to the exact size specified. Scale output to `width` x `height`. By default, the resized image is cropped to the exact size specified.
`width` is the Number of pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height. `width` is the integral Number of pixels wide the resultant image should be, between 1 and 16383 (0x3FFF). Use `null` or `undefined` to auto-scale the width to match the height.
`height` is the Number of pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width. `height` is the integral Number of pixels high the resultant image should be, between 1 and 16383. Use `null` or `undefined` to auto-scale the height to match the width.
#### extract(top, left, width, height) #### extract(top, left, width, height)

View File

@ -361,7 +361,7 @@ Sharp.prototype.resize = function(width, height) {
if (!width) { if (!width) {
this.options.width = -1; this.options.width = -1;
} else { } else {
if (typeof width === 'number' && !Number.isNaN(width)) { if (typeof width === 'number' && !Number.isNaN(width) && width % 1 === 0 && width > 0 && width <= 0x3FFF) {
this.options.width = width; this.options.width = width;
} else { } else {
throw new Error('Invalid width ' + width); throw new Error('Invalid width ' + width);
@ -370,7 +370,7 @@ Sharp.prototype.resize = function(width, height) {
if (!height) { if (!height) {
this.options.height = -1; this.options.height = -1;
} else { } else {
if (typeof height === 'number' && !Number.isNaN(height)) { if (typeof height === 'number' && !Number.isNaN(height) && height % 1 === 0 && height > 0 && height <= 0x3FFF) {
this.options.height = height; this.options.height = height;
} else { } else {
throw new Error('Invalid height ' + height); throw new Error('Invalid height ' + height);

View File

@ -176,7 +176,7 @@ describe('Image metadata', function() {
assert.strictEqual(3, metadata.channels); assert.strictEqual(3, metadata.channels);
assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(false, metadata.hasProfile);
assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(false, metadata.hasAlpha);
image.resize(metadata.width / 2).toBuffer(function(err, data, info) { image.resize(Math.floor(metadata.width / 2)).toBuffer(function(err, data, info) {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);
assert.strictEqual(1362, info.width); assert.strictEqual(1362, info.width);

View File

@ -64,7 +64,7 @@ describe('Resize dimensions', function() {
}); });
}); });
it('Invalid width', function(done) { it('Invalid width - NaN', function(done) {
var isValid = true; var isValid = true;
try { try {
sharp(fixtures.inputJpg).resize('spoons', 240); sharp(fixtures.inputJpg).resize('spoons', 240);
@ -75,7 +75,7 @@ describe('Resize dimensions', function() {
done(); done();
}); });
it('Invalid height', function(done) { it('Invalid height - NaN', function(done) {
var isValid = true; var isValid = true;
try { try {
sharp(fixtures.inputJpg).resize(320, 'spoons'); sharp(fixtures.inputJpg).resize(320, 'spoons');
@ -86,6 +86,50 @@ describe('Resize dimensions', function() {
done(); done();
}); });
it('Invalid width - float', function(done) {
var isValid = true;
try {
sharp(fixtures.inputJpg).resize(1.5, 240);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
it('Invalid height - float', function(done) {
var isValid = true;
try {
sharp(fixtures.inputJpg).resize(320, 1.5);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
it('Invalid width - too large', function(done) {
var isValid = true;
try {
sharp(fixtures.inputJpg).resize(0x4000, 240);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
it('Invalid height - too large', function(done) {
var isValid = true;
try {
sharp(fixtures.inputJpg).resize(320, 0x4000);
} catch (err) {
isValid = false;
}
assert.strictEqual(false, isValid);
done();
});
it('TIFF embed known to cause rounding errors', function(done) { it('TIFF embed known to cause rounding errors', function(done) {
sharp(fixtures.inputTiff).resize(240, 320).embed().jpeg().toBuffer(function(err, data, info) { sharp(fixtures.inputTiff).resize(240, 320).embed().jpeg().toBuffer(function(err, data, info) {
if (err) throw err; if (err) throw err;