Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return width and/or height from image url spec #62

Open
thijs-justacodename opened this issue Oct 24, 2023 · 0 comments
Open

Return width and/or height from image url spec #62

thijs-justacodename opened this issue Oct 24, 2023 · 0 comments

Comments

@thijs-justacodename
Copy link

We would like to render images with a maximum width and/or height, and set the width and height attributes on the resulting <img> tag. An easier example would be this:

Image asset with id:
image-38704787082d878c983f9327082be0033ae295bb-698x1056-png

We would like to render that image at a width of 200px, but we don't know the exact height of the image. So we end up with an <img src="..." width="200" /> and without a height set.

Another example would be using the same image, and requesting a w=200&h=150. The resulting image has a width of 99 and a height of 150, but we would be wrong to generate an element like this:

<img src="...image-38704787082d878c983f9327082be0033ae295bb-698x1056-png?w=200&h=150&fit=fill" width="200" height="150" />

We could of course parse the asset ID to find the original image size, and/or use the image metadata to calculate this, but that does not feel scalable as any crop options might apply. All of the logic we would need for that is already being done in the urlForImage() function.

Describe the solution you'd like
We would like a spec method (or equivalent) on imageBuilder to retrieve the calculated specifications that contains the calculated width, height and/or aspect ratio, if it was possible to calculate it.

Describe alternatives you've considered

  • Duplicating the logic in imageBuilder to calculate the same
  • Parsing the resulting URL for information

Additional context
For proper page load performance it is important to set width and height explicitly on <img> elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant