diff --git a/src/pipeline.cc b/src/pipeline.cc index 2a341782..864555b8 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -598,12 +598,19 @@ class PipelineWorker : public AsyncWorker { baton->background[2] * multiplier }; // Add alpha channel to background colour - if (HasAlpha(image)) { + if (baton->background[3] < 255.0 || HasAlpha(image)) { background.push_back(baton->background[3] * multiplier); } + // Add non-transparent alpha channel, if required + if (baton->background[3] < 255.0 && !HasAlpha(image)) { + image = image.bandjoin( + VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier) + ); + } // Embed baton->width = image.width() + baton->extendLeft + baton->extendRight; baton->height = image.height() + baton->extendTop + baton->extendBottom; + image = image.embed(baton->extendLeft, baton->extendTop, baton->width, baton->height, VImage::option()->set("extend", VIPS_EXTEND_BACKGROUND)->set("background", background)); } diff --git a/test/fixtures/expected/addAlphaChanelBeforeExtend.png b/test/fixtures/expected/addAlphaChanelBeforeExtend.png new file mode 100644 index 00000000..ebb7b0c4 Binary files /dev/null and b/test/fixtures/expected/addAlphaChanelBeforeExtend.png differ diff --git a/test/unit/extend.js b/test/unit/extend.js index 2232512d..ca48d350 100644 --- a/test/unit/extend.js +++ b/test/unit/extend.js @@ -49,4 +49,16 @@ describe('Extend', function () { }); }); + it('should add alpha channel before extending with a transparent Background', function( done ){ + sharp(fixtures.inputJpgWithLandscapeExif1) + .background({r: 0, g: 0, b: 0, a: 0}) + .toFormat( sharp.format.png ) + .extend({top: 0, bottom: 10, left: 0, right: 10}) + .toBuffer( function(err, data, info){ + assert.strictEqual(610, info.width); + assert.strictEqual(460, info.height); + fixtures.assertSimilar(fixtures.expected('addAlphaChanelBeforeExtend.png'), data, done); + }); + }); + });