Autoconvert GIF+SVG input to PNG output if no format specified

This commit is contained in:
Lovell Fuller 2016-11-30 22:46:04 +00:00
parent 93e14484da
commit 7231d92d1f
5 changed files with 22 additions and 11 deletions

View File

@ -24,7 +24,9 @@ Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
# toBuffer # toBuffer
Write output to a Buffer. 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: `callback`, if present, gets three arguments `(err, buffer, info)` where:
- `err` is an error message, if any. - `err` is an error message, if any.

View File

@ -15,6 +15,8 @@ Requires libvips v8.4.2.
Access to these is now via output format functions, for example `quality(n)` Access to these is now via output format functions, for example `quality(n)`
is now `jpeg({quality: n})` and/or `webp({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. * Expose libvips' "centre" resize option to mimic \*magick's +0.5px convention.
[#568](https://github.com/lovell/sharp/issues/568) [#568](https://github.com/lovell/sharp/issues/568)

View File

@ -45,7 +45,9 @@ const toFile = function toFile (fileOut, callback) {
/** /**
* Write output to a Buffer. * 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: * `callback`, if present, gets three arguments `(err, buffer, info)` where:
* - `err` is an error message, if any. * - `err` is an error message, if any.
* - `buffer` is the output image data. * - `buffer` is the output image data.

View File

@ -743,7 +743,8 @@ class PipelineWorker : public Nan::AsyncWorker {
} else { } else {
baton->channels = std::min(baton->channels, 3); 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 // Strip profile
if (!baton->withMetadata) { if (!baton->withMetadata) {
vips_image_remove(image.get_image(), VIPS_META_ICC_NAME); vips_image_remove(image.get_image(), VIPS_META_ICC_NAME);
@ -823,7 +824,8 @@ class PipelineWorker : public Nan::AsyncWorker {
); );
baton->formatOut = "jpeg"; baton->formatOut = "jpeg";
baton->channels = std::min(baton->channels, 3); 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 // Strip profile
if (!baton->withMetadata) { if (!baton->withMetadata) {
vips_image_remove(image.get_image(), VIPS_META_ICC_NAME); vips_image_remove(image.get_image(), VIPS_META_ICC_NAME);

View File

@ -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) sharp(fixtures.inputGif)
.resize(320, 80) .resize(320, 80)
.toFile(fixtures.outputZoinks, function (err) { .toFile(fixtures.outputZoinks, function (err, info) {
assert(!!err); 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(); 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) sharp(fixtures.inputSvgWithEmbeddedImages)
.png()
.toBuffer(function (err, data, info) { .toBuffer(function (err, data, info) {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); 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) sharp(fixtures.inputGifGreyPlusAlpha)
.resize(8, 4) .resize(8, 4)
.png()
.toBuffer(function (err, data, info) { .toBuffer(function (err, data, info) {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, data.length > 0); assert.strictEqual(true, data.length > 0);