@@ -13,25 +13,46 @@ type Props = PtProps<ImageNode>;
1313
1414const { node } = Astro .props ;
1515
16- const src = Astro .locals .sanity
17- .urlForImage (node )
18- ?.width (1920 )
19- .height (1080 )
20- .url ();
16+ // Parse intrinsic dimensions from Sanity asset _ref ("image-<hash>-<width>x<height>-<ext>")
17+ // so we never pass a forced 16:9 .height(1080) to @sanity/image-url (which causes
18+ // @sanity/image-url to inject ?rect=... and crop tall/square images like memes or diagrams).
19+ const assetRef = stegaClean (
20+ (node as { asset? : { _ref? : string } })?.asset ?._ref ,
21+ );
22+ const dimMatch = assetRef ?.match (/ -(\d + )x(\d + )-[a-z0-9 ] + $ / i );
23+ const intrinsicWidth = dimMatch ? Number (dimMatch [1 ]) : 1200 ;
24+ const intrinsicHeight = dimMatch ? Number (dimMatch [2 ]) : 675 ;
25+
26+ const builder = Astro .locals .sanity .urlForImage (node );
27+ const maxWidth = Math .min (intrinsicWidth , 1600 );
28+ const src = builder ?.width (maxWidth ).quality (82 ).url ();
29+
30+ const responsiveWidths = [480 , 640 , 800 , 960 , 1200 , 1600 ].filter (
31+ (w ) => w <= maxWidth ,
32+ );
33+ if (! responsiveWidths .includes (maxWidth )) {
34+ responsiveWidths .push (maxWidth );
35+ }
36+ const srcset = builder
37+ ? responsiveWidths
38+ .map ((w ) => ` ${builder .width (w ).quality (82 ).url ()} ${w }w ` )
39+ .join (" , " )
40+ : undefined ;
2141
2242const alt = stegaClean (node .alt ) ?? " " ;
2343const caption = node .caption ;
2444---
2545
2646{
2747 src && (
28- <figure class = " not-prose my-8" >
48+ <figure class = " not-prose my-8 flex flex-col items-center " >
2949 <img
30- class = " w- full h-auto rounded-md shadow-md"
50+ class = " h-auto w-auto max-w- full max-h-[80dvh] rounded-md shadow-md"
3151 src = { src }
32- width = { 1920 }
33- height = { 1080 }
34- sizes = " 100vw"
52+ srcset = { srcset }
53+ width = { intrinsicWidth }
54+ height = { intrinsicHeight }
55+ sizes = " (max-width: 896px) 100vw, 896px"
3556 alt = { alt }
3657 loading = " lazy"
3758 decoding = " async"
0 commit comments