Add toFormat 'jpg' alias for 'jpeg' (#814)

This commit is contained in:
jingsam 2017-05-22 19:59:43 +08:00 committed by Lovell Fuller
parent 99810c0311
commit 4c6804eadc
2 changed files with 16 additions and 0 deletions

View File

@ -274,6 +274,7 @@ function toFormat (format, options) {
if (is.object(format) && is.string(format.id)) {
format = format.id;
}
if (format === 'jpg') format = 'jpeg';
if (!is.inArray(format, ['jpeg', 'png', 'webp', 'tiff', 'raw'])) {
throw new Error('Unsupported output format ' + format);
}

View File

@ -253,6 +253,21 @@ describe('Input/output', function () {
});
});
it('Support output to jpg format', function (done) {
sharp(fixtures.inputPng)
.resize(320, 240)
.toFormat('jpg')
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual(data.length, info.size);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
done();
});
});
it('Fail when output File is input File', function (done) {
sharp(fixtures.inputJpg).toFile(fixtures.inputJpg, function (err) {
assert(!!err);