Add toFormat convenience method #137

This commit is contained in:
Lovell Fuller 2015-02-12 11:37:37 +00:00
parent 96dd40cee1
commit 8f41fed9c2
4 changed files with 75 additions and 10 deletions

View File

@ -197,7 +197,7 @@ sharp('input.gif')
.resize(200, 300) .resize(200, 300)
.background({r: 0, g: 0, b: 0, a: 0}) .background({r: 0, g: 0, b: 0, a: 0})
.embed() .embed()
.webp() .toFormat(sharp.format.webp)
.toBuffer(function(err, outputBuffer) { .toBuffer(function(err, outputBuffer) {
if (err) { if (err) {
throw err; throw err;
@ -211,7 +211,7 @@ sharp('input.gif')
sharp(inputBuffer) sharp(inputBuffer)
.resize(200, 200) .resize(200, 200)
.max() .max()
.jpeg() .toFormat('jpeg')
.toBuffer().then(function(outputBuffer) { .toBuffer().then(function(outputBuffer) {
// outputBuffer contains JPEG image data no wider than 200 pixels and no higher // outputBuffer contains JPEG image data no wider than 200 pixels and no higher
// than 200 pixels regardless of the inputBuffer image dimensions // than 200 pixels regardless of the inputBuffer image dimensions
@ -415,6 +415,13 @@ The number of channels depends on the input image and selected options.
* 3 channels for colour images without alpha transparency, with bytes ordered \[red, green, blue, red, green, blue, etc.\]). * 3 channels for colour images without alpha transparency, with bytes ordered \[red, green, blue, red, green, blue, etc.\]).
* 4 channels for colour images with alpha transparency, with bytes ordered \[red, green, blue, alpha, red, green, blue, alpha, etc.\]. * 4 channels for colour images with alpha transparency, with bytes ordered \[red, green, blue, alpha, red, green, blue, alpha, etc.\].
#### toFormat(format)
Convenience method for the above output format methods, where `format` is either:
* an attribute of the `sharp.format` Object e.g. `sharp.format.jpeg`, or
* a String containing `jpeg`, `png`, `webp` or `raw`.
#### quality(quality) #### quality(quality)
The output quality to use for lossy JPEG, WebP and TIFF output formats. The default quality is `80`. The output quality to use for lossy JPEG, WebP and TIFF output formats. The default quality is `80`.

View File

@ -435,25 +435,40 @@ Sharp.prototype.toFile = function(output, callback) {
return this; return this;
}; };
/*
Write output to a Buffer
*/
Sharp.prototype.toBuffer = function(callback) { Sharp.prototype.toBuffer = function(callback) {
return this._sharp(callback); return this._sharp(callback);
}; };
/*
Force JPEG output
*/
Sharp.prototype.jpeg = function() { Sharp.prototype.jpeg = function() {
this.options.output = '__jpeg'; this.options.output = '__jpeg';
return this; return this;
}; };
/*
Force PNG output
*/
Sharp.prototype.png = function() { Sharp.prototype.png = function() {
this.options.output = '__png'; this.options.output = '__png';
return this; return this;
}; };
/*
Force WebP output
*/
Sharp.prototype.webp = function() { Sharp.prototype.webp = function() {
this.options.output = '__webp'; this.options.output = '__webp';
return this; return this;
}; };
/*
Force raw, uint8 output
*/
Sharp.prototype.raw = function() { Sharp.prototype.raw = function() {
if (semver.gte(libvipsVersion, '7.42.0')) { if (semver.gte(libvipsVersion, '7.42.0')) {
this.options.output = '__raw'; this.options.output = '__raw';
@ -463,6 +478,23 @@ Sharp.prototype.raw = function() {
return this; return this;
}; };
/*
Force output to a given format
*/
module.exports.format = {'jpeg': 'jpeg', 'png': 'png', 'webp': 'webp', 'raw': 'raw'};
Sharp.prototype.toFormat = function(format) {
if (
typeof format === 'string' &&
typeof module.exports.format[format] === 'string' &&
typeof this[format] === 'function'
) {
this[format]();
} else {
throw new Error('Unsupported format ' + format);
}
return this;
};
/* /*
Used by a Writable Stream to notify that it is ready for data Used by a Writable Stream to notify that it is ready for data
*/ */

View File

@ -34,10 +34,10 @@
"vips" "vips"
], ],
"dependencies": { "dependencies": {
"bluebird": "^2.9.7", "bluebird": "^2.9.8",
"color": "^0.7.3", "color": "^0.7.3",
"nan": "^1.6.2", "nan": "^1.6.2",
"semver": "^4.2.0" "semver": "^4.2.2"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^2.1.0", "mocha": "^2.1.0",

View File

@ -135,10 +135,11 @@ describe('Input/output', function() {
readableButNotAnImage.pipe(writable); readableButNotAnImage.pipe(writable);
}); });
it('Sequential read', function(done) { it('Sequential read, force JPEG', function(done) {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.sequentialRead() .sequentialRead()
.resize(320, 240) .resize(320, 240)
.toFormat(sharp.format.jpeg)
.toBuffer(function(err, data, info) { .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);
@ -150,10 +151,11 @@ describe('Input/output', function() {
}); });
}); });
it('Not sequential read', function(done) { it('Not sequential read, force JPEG', function(done) {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.sequentialRead(false) .sequentialRead(false)
.resize(320, 240) .resize(320, 240)
.toFormat('jpeg')
.toBuffer(function(err, data, info) { .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);
@ -292,6 +294,30 @@ describe('Input/output', function() {
}); });
}); });
it('WebP output', function(done) {
sharp(fixtures.inputJpg)
.resize(320, 240)
.toFormat(sharp.format.webp)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
done();
});
});
it('Invalid output format', function(done) {
var isValid = false;
try {
sharp().toFormat('zoinks');
isValid = true;
} catch (e) {}
assert(!isValid);
done();
});
it('File input with corrupt header fails gracefully', function(done) { it('File input with corrupt header fails gracefully', function(done) {
sharp(fixtures.inputJpgWithCorruptHeader) sharp(fixtures.inputJpgWithCorruptHeader)
.toBuffer(function(err) { .toBuffer(function(err) {
@ -436,7 +462,7 @@ describe('Input/output', function() {
it('Convert SVG, if supported, to PNG', function(done) { it('Convert SVG, if supported, to PNG', function(done) {
sharp(fixtures.inputSvg) sharp(fixtures.inputSvg)
.resize(100, 100) .resize(100, 100)
.png() .toFormat('png')
.toFile(fixtures.path('output.svg.png'), function(err, info) { .toFile(fixtures.path('output.svg.png'), function(err, info) {
if (err) { if (err) {
assert.strictEqual('Input file is of an unsupported image format', err.message); assert.strictEqual('Input file is of an unsupported image format', err.message);
@ -453,7 +479,7 @@ describe('Input/output', function() {
it('Convert PSD to PNG', function(done) { it('Convert PSD to PNG', function(done) {
sharp(fixtures.inputPsd) sharp(fixtures.inputPsd)
.resize(320, 240) .resize(320, 240)
.png() .toFormat(sharp.format.png)
.toFile(fixtures.path('output.psd.png'), function(err, info) { .toFile(fixtures.path('output.psd.png'), function(err, info) {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, info.size > 0); assert.strictEqual(true, info.size > 0);
@ -501,7 +527,7 @@ describe('Input/output', function() {
it('3 channel colour image without transparency', function(done) { it('3 channel colour image without transparency', function(done) {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(32, 24) .resize(32, 24)
.raw() .toFormat('raw')
.toBuffer(function(err, data, info) { .toBuffer(function(err, data, info) {
assert.strictEqual(32 * 24 * 3, info.size); assert.strictEqual(32 * 24 * 3, info.size);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);
@ -514,7 +540,7 @@ describe('Input/output', function() {
it('4 channel colour image with transparency', function(done) { it('4 channel colour image with transparency', function(done) {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(32, 24) .resize(32, 24)
.raw() .toFormat(sharp.format.raw)
.toBuffer(function(err, data, info) { .toBuffer(function(err, data, info) {
assert.strictEqual(32 * 24 * 4, info.size); assert.strictEqual(32 * 24 * 4, info.size);
assert.strictEqual(data.length, info.size); assert.strictEqual(data.length, info.size);