diff --git a/docs/api-output.md b/docs/api-output.md index fe8a8fe9..dd065b30 100644 --- a/docs/api-output.md +++ b/docs/api-output.md @@ -24,7 +24,9 @@ Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe # toBuffer Write output to a Buffer. -By default, the format will match the input image. JPEG, PNG, WebP, and RAW are supported. +JPEG, PNG, WebP, and RAW output are supported. +By default, the format will match the input image, except GIF and SVG input which become PNG output. + `callback`, if present, gets three arguments `(err, buffer, info)` where: - `err` is an error message, if any. diff --git a/docs/changelog.md b/docs/changelog.md index 062c7118..b68325c8 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -15,6 +15,8 @@ Requires libvips v8.4.2. Access to these is now via output format functions, for example `quality(n)` is now `jpeg({quality: n})` and/or `webp({quality: n})`. +* Autoconvert GIF and SVG input to PNG output if no other format is specified. + * Expose libvips' "centre" resize option to mimic \*magick's +0.5px convention. [#568](https://github.com/lovell/sharp/issues/568) diff --git a/lib/output.js b/lib/output.js index ee498d3b..b164af75 100644 --- a/lib/output.js +++ b/lib/output.js @@ -45,7 +45,9 @@ const toFile = function toFile (fileOut, callback) { /** * Write output to a Buffer. - * By default, the format will match the input image. JPEG, PNG, WebP, and RAW are supported. + * JPEG, PNG, WebP, and RAW output are supported. + * By default, the format will match the input image, except GIF and SVG input which become PNG output. + * * `callback`, if present, gets three arguments `(err, buffer, info)` where: * - `err` is an error message, if any. * - `buffer` is the output image data. diff --git a/src/pipeline.cc b/src/pipeline.cc index 83fee51b..06093d85 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -743,7 +743,8 @@ class PipelineWorker : public Nan::AsyncWorker { } else { baton->channels = std::min(baton->channels, 3); } - } else if (baton->formatOut == "png" || (baton->formatOut == "input" && inputImageType == ImageType::PNG)) { + } else if (baton->formatOut == "png" || (baton->formatOut == "input" && + (inputImageType == ImageType::PNG || inputImageType == ImageType::GIF || inputImageType == ImageType::SVG))) { // Strip profile if (!baton->withMetadata) { vips_image_remove(image.get_image(), VIPS_META_ICC_NAME); @@ -823,7 +824,8 @@ class PipelineWorker : public Nan::AsyncWorker { ); baton->formatOut = "jpeg"; baton->channels = std::min(baton->channels, 3); - } else if (baton->formatOut == "png" || isPng || (matchInput && inputImageType == ImageType::PNG)) { + } else if (baton->formatOut == "png" || isPng || (matchInput && + (inputImageType == ImageType::PNG || inputImageType == ImageType::GIF || inputImageType == ImageType::SVG))) { // Strip profile if (!baton->withMetadata) { vips_image_remove(image.get_image(), VIPS_META_ICC_NAME); diff --git a/test/unit/io.js b/test/unit/io.js index cc63c39b..c46b872f 100644 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -457,11 +457,16 @@ describe('Input/output', function () { }); }); - it('Match GIF input, therefore fail', function (done) { + it('Autoconvert GIF input to PNG output', function (done) { sharp(fixtures.inputGif) .resize(320, 80) - .toFile(fixtures.outputZoinks, function (err) { - assert(!!err); + .toFile(fixtures.outputZoinks, function (err, info) { + if (err) throw err; + assert.strictEqual(true, info.size > 0); + assert.strictEqual('png', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(80, info.height); + fs.unlinkSync(fixtures.outputZoinks); done(); }); }); @@ -696,9 +701,8 @@ describe('Input/output', function () { }); }); - it('Convert SVG with embedded images to PNG, respecting dimensions', function (done) { + it('Convert SVG with embedded images to PNG, respecting dimensions, autoconvert to PNG', function (done) { sharp(fixtures.inputSvgWithEmbeddedImages) - .png() .toBuffer(function (err, data, info) { if (err) throw err; assert.strictEqual('png', info.format); @@ -772,10 +776,9 @@ describe('Input/output', function () { }); }); - it('Load GIF grey+alpha from file', function (done) { + it('Load GIF grey+alpha from file, auto convert to PNG', function (done) { sharp(fixtures.inputGifGreyPlusAlpha) .resize(8, 4) - .png() .toBuffer(function (err, data, info) { if (err) throw err; assert.strictEqual(true, data.length > 0);