Some PNGs result in a black alpha channel, most don't.
See original GitHub issueI have a REST call on my server that takes in a buffer of an image sent from the client along with information for cropping.
The code works 99% of the time but every so often I encounter a PNG image that does not preserve the alpha channel and it ends up black.
This is the (simplified) code:
(github does not want to format this code nicely for some reason… so here is a screenshot as well)
if (fileType === 'jpeg' || fileType === 'jpg') { console.log('Is jpg making backgroung alpha 1'); s = s.background({r: 255, g: 255, b: 255, alpha: 1}); } else { s = s.background({r: 255, g: 255, b: 255, alpha: 0}); s = s.png({progressive: true, force: true}); } s = s.extend(padding); if (fileType === 'jpeg' || fileType === 'jpg') { s = s.flatten(); } s .toBuffer((err, buffer) => { s = sharp(buffer); } }
I do some cropping and calculating variable
padding
prior to that.
PNG it works with:
PNG it does not work with:
This is the result of the second one after running it through the code.
I cannot figure out what is going on, maybe some more knowledgeable help on the subject or the PNG fits an edge case?
Note: the call to .png() was added today to see if forcing it to png would help, it didn’t, and has been working for most PNGs without it up till now.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
@mjgallag I just updated to 0.21.0 and unfortunately it seems that the “swinging with the starts” image still comes out black. Even after moving the deprecated background call to the .extends call I had below.
Closing due to inactivity but please feel free to re-open with more details, including a standalone code sample, if the problem persists.