The AMP way to achieve that is by using the layout
attribute. In amp-img, you specify the source, the native height and weight ; sourceset with formats, in "css pixels"(e.g. 96px/in) for your responsive image with layout= responsive
and you're done. AMP will maintain the h/w ratio.
Ex.
<amp-img
src="/img/small.jpg"
srcset="/img/medium.jpg 640w,
/img/small.jpg 320w"
width="1800"
height="2777"
layout="responsive"
alt="Don't forget to add one">
</amp-img>
In this example, the layout="responsive"
would be inferred by the AMP engine because image size and formats are specified. When height and width are not defined, the default layout is "container" (e.g. like plain div
).
You can also include amp media queries in amp-img. The preceding example could be set like this:
<amp-img
media="(max-width: 639px)"
src="/img/small.jpg"
width="450"
height="694"
layout="responsive"
alt="Don't forget to add one">
</amp-img>
<amp-img
media="(min-width: 639px)"
src="/img/medium.jpg"
width="900"
height="1388"
layout="responsive"
alt="Don't forget to add one">
</amp-img>
In this case, the "media" attribute will have precedence over the adaptative selection (but it makes almost no practical differences except if you want to force a certain image upon a certain screen size).
For a good overview: https://www.ampbyexample.com/advanced/layout_system/
Hope this help