mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
Add toFormat convenience method #137
This commit is contained in:
parent
96dd40cee1
commit
8f41fed9c2
11
README.md
11
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`.
|
||||
|
32
index.js
32
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
|
||||
*/
|
||||
|
@ -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",
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user