Sanity Pioneers: Get early access to betas, extra AI credits, and a direct line to the engineering team. Apply now

How to use the Sanity CDN to Transform SVG Images to Bitmap/PNG Images

4 repliesLast updated: Nov 12, 2021

is it possible to use the Sanity CDN to transform SVG images to bitmap/png images? My PDF generator hates SVG images
Very cool, how do I do this in my GROQ query? Any ways to to say “If svg, then convert to png/jpg” or do I have to find every place an SVG may come and convert all the images to png or jpg?
I can use the URL, but my current setup works using GROQ
Very sexy, how do I do this in my GROQ query? Any ways to to say “If svg, then convert to png/jpg” or do I have to find every place an SVG may come and convert all the images to png or jpg?
Depends on how your query is, but in theory yes. Something like this:

*[_type == 'blogPost'] [0...10] {
  images[] {
    asset->{
      url,
      extension == 'svg' => {
        'url': url + '?fm=png'
      }
    }
  }
}
The relevant part here is the 'select the url, and if the extension of the asset is svg, assign url to be the url + a query parameter'

Was this answer helpful?

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.

Related contributions