Issue with bg parameter in Sanity image URL library for converting PNG to JPG
Great to hear you're enjoying Sanity! I can help with your image transformation question about the bg parameter.
Yes, the bg parameter should definitely work when converting transparent PNGs to JPG format. According to the Sanity documentation, the bg parameter is designed to "fill in any transparent areas in the image with a color" and accepts hexadecimal color values (RGB, ARGB, RRGGBB, or AARRGGBB).
The black background you're seeing is actually the default behavior when converting transparent images to JPG without specifying a background color. Here are some things to check:
1. Verify your parameter format
Make sure your hex color doesn't include the # symbol - just use the hex value directly:
- ✅ Correct:
?fm=jpg&bg=ffffff(for white) - ✅ Correct:
?fm=jpg&bg=ff0000(for red) - ❌ Wrong:
?fm=jpg&bg=#ffffff
2. Example URL
Here's what a working URL should look like:
https://cdn.sanity.io/images/[project]/[dataset]/[asset].png?fm=jpg&bg=ffffff
This will convert your PNG to JPG and fill transparent areas with white instead of black.
3. Using the image-url helper
If you're using the @sanity/image-url helper library, you can chain methods like this:
imageUrlBuilder(client)
.image(yourImage)
.format('jpg')
.bg('ffffff')
.url()4. Check for caching
Since the Sanity CDN caches transformed images, you might be seeing a cached version with the old black background. Try adding a cache-busting parameter or wait a bit for the cache to clear. You could also test with a slightly different size parameter to force a new transformation.
The bg parameter works independently and doesn't require any specific fit mode or other parameters - it simply fills transparent areas with your specified color whenever the image format doesn't support transparency (like JPG).
If you're still seeing black backgrounds after trying these suggestions, double-check that you're actually hitting the new URL with the bg parameter included. Hope this helps!
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.