Skip to content

Commit

Permalink
Autoconvert GIF+SVG input to PNG output if no format specified
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Nov 30, 2016
1 parent 93e1448 commit 7231d92
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
4 changes: 3 additions & 1 deletion docs/api-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions test/unit/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7231d92

Please sign in to comment.