From 8f41fed9c2b33ee3b26cf0caf1d45b95e73e18df Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Thu, 12 Feb 2015 11:37:37 +0000 Subject: [PATCH] Add toFormat convenience method #137 --- README.md | 11 +++++++++-- index.js | 32 ++++++++++++++++++++++++++++++++ package.json | 4 ++-- test/unit/io.js | 38 ++++++++++++++++++++++++++++++++------ 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 957280a0..0f4d99b9 100755 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ sharp('input.gif') .resize(200, 300) .background({r: 0, g: 0, b: 0, a: 0}) .embed() - .webp() + .toFormat(sharp.format.webp) .toBuffer(function(err, outputBuffer) { if (err) { throw err; @@ -211,7 +211,7 @@ sharp('input.gif') sharp(inputBuffer) .resize(200, 200) .max() - .jpeg() + .toFormat('jpeg') .toBuffer().then(function(outputBuffer) { // outputBuffer contains JPEG image data no wider than 200 pixels and no higher // 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.\]). * 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) The output quality to use for lossy JPEG, WebP and TIFF output formats. The default quality is `80`. diff --git a/index.js b/index.js index dc74a48b..89f2a4fe 100755 --- a/index.js +++ b/index.js @@ -435,25 +435,40 @@ Sharp.prototype.toFile = function(output, callback) { return this; }; +/* + Write output to a Buffer +*/ Sharp.prototype.toBuffer = function(callback) { return this._sharp(callback); }; +/* + Force JPEG output +*/ Sharp.prototype.jpeg = function() { this.options.output = '__jpeg'; return this; }; +/* + Force PNG output +*/ Sharp.prototype.png = function() { this.options.output = '__png'; return this; }; +/* + Force WebP output +*/ Sharp.prototype.webp = function() { this.options.output = '__webp'; return this; }; +/* + Force raw, uint8 output +*/ Sharp.prototype.raw = function() { if (semver.gte(libvipsVersion, '7.42.0')) { this.options.output = '__raw'; @@ -463,6 +478,23 @@ Sharp.prototype.raw = function() { 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 */ diff --git a/package.json b/package.json index d67e3820..18438746 100644 --- a/package.json +++ b/package.json @@ -34,10 +34,10 @@ "vips" ], "dependencies": { - "bluebird": "^2.9.7", + "bluebird": "^2.9.8", "color": "^0.7.3", "nan": "^1.6.2", - "semver": "^4.2.0" + "semver": "^4.2.2" }, "devDependencies": { "mocha": "^2.1.0", diff --git a/test/unit/io.js b/test/unit/io.js index 635b100d..498da21f 100755 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -135,10 +135,11 @@ describe('Input/output', function() { readableButNotAnImage.pipe(writable); }); - it('Sequential read', function(done) { + it('Sequential read, force JPEG', function(done) { sharp(fixtures.inputJpg) .sequentialRead() .resize(320, 240) + .toFormat(sharp.format.jpeg) .toBuffer(function(err, data, info) { if (err) throw err; 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) .sequentialRead(false) .resize(320, 240) + .toFormat('jpeg') .toBuffer(function(err, data, info) { if (err) throw err; 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) { sharp(fixtures.inputJpgWithCorruptHeader) .toBuffer(function(err) { @@ -436,7 +462,7 @@ describe('Input/output', function() { it('Convert SVG, if supported, to PNG', function(done) { sharp(fixtures.inputSvg) .resize(100, 100) - .png() + .toFormat('png') .toFile(fixtures.path('output.svg.png'), function(err, info) { if (err) { 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) { sharp(fixtures.inputPsd) .resize(320, 240) - .png() + .toFormat(sharp.format.png) .toFile(fixtures.path('output.psd.png'), function(err, info) { if (err) throw err; assert.strictEqual(true, info.size > 0); @@ -501,7 +527,7 @@ describe('Input/output', function() { it('3 channel colour image without transparency', function(done) { sharp(fixtures.inputJpg) .resize(32, 24) - .raw() + .toFormat('raw') .toBuffer(function(err, data, info) { assert.strictEqual(32 * 24 * 3, 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) { sharp(fixtures.inputPngWithTransparency) .resize(32, 24) - .raw() + .toFormat(sharp.format.raw) .toBuffer(function(err, data, info) { assert.strictEqual(32 * 24 * 4, info.size); assert.strictEqual(data.length, info.size);