Skip to content

Commit

Permalink
Add toFormat convenience method #137
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Feb 12, 2015
1 parent 96dd40c commit 8f41fed
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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`.
Expand Down
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 32 additions & 6 deletions test/unit/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 8f41fed

Please sign in to comment.