How to Set Aspect Ratios on Images?

5 replies
Last updated: Sep 7, 2020
Hey, quick one - is there anyway I can get info about the dimensions of image that are being uploaded? Want to use them to set some aspect ratio styling. Thanks!
Hey Callum, that information is stored in the image asset object under
metadata.dimensions
. You can query it like this for example:
*[_type == "post"] {
  _id,
  "image":image.asset->
  {
    "dimensions": metadata.dimensions
  }
}
Great thank you! My images are somewhat more nested than that but will see if I can get it to work
Let me know if you need any help navigating the nesting πŸ™‚
Got there in the end just took a bit of groq revision πŸ˜… – thanks again!
Awesome - and we approve of groq revisions πŸ€“

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.

Was this answer helpful?