1


I have a base64 image data which I want to save a png file. But When I save it as file, it does not retain the transparent portions but becomes fully black.
Is there a way to retain transparency while writing png file in Nodejs?

Sandy
  • 2,253
  • 6
  • 24
  • 33
  • How are you converting to png? Any libs or are you doing it yourself? – Sello Mkantjwa Dec 13 '18 at 06:37
  • fs.writeFileSync(name, data, "base64"); I just used this. – Sandy Dec 13 '18 at 06:42
  • So you saved the base64 string and saved it with an extension of .png?? – Sello Mkantjwa Dec 13 '18 at 08:09
  • yes. Thats right. I get base64 string from one of my API calls, which I want to save as png with transparency. – Sandy Dec 13 '18 at 08:12
  • That is not the correct way to go about it. You need to convert that base64 to png in and then save it as such. The fact that the image has the png extension does not mean that the contents are a valid png image. try something along the lines of: https://stackoverflow.com/a/38936042/2212208 before saving the image – Sello Mkantjwa Dec 13 '18 at 08:20
  • Ok Thanks. Is there a way to check is incoming base64 data is a valid PNG image? – Sandy Dec 13 '18 at 08:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185196/discussion-between-sello-mkantjwa-and-sandy). – Sello Mkantjwa Dec 13 '18 at 09:20

0 Answers0