node.jsjpegtransparencysharp

nodejs Sharp: transparent into white


I am using Nodejs Sharp to transcode/resize png images into jpg. Is there way to replace transparent with white (or other light color) rather than black? I found solution for an older library but Sharp seems to be fastest and greatest.

.background does not work

.then( data => Sharp(data.Body)
  .resize(SIZES[resize_type].width, SIZES[resize_type].height)
  .max()
  .withoutEnlargement()
  .background("white")
  .toFormat('jpeg')
  .toBuffer()
)

Solution

  • by the document, we should do the way as Msalam suggest but unluckily that is not enough. I figured out We should add .flatten(true) before .resize(...) to make it work correctly.