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

Proposal: NextImage should automatically determine width/height for local images #18497

Closed
Timer opened this issue Oct 29, 2020 · 15 comments
Closed
Milestone

Comments

@Timer
Copy link
Member

Timer commented Oct 29, 2020

Feature request

It'd be really cool if NextImage automatically configured width and height for local images, as well as automatically hashing the image.

Describe the solution you'd like

import Logo from "../logo.png";
import Image from "next/image";

// Logo = { src: '<basePath>/_next/media/logo-<hash>.png', width: 200, height: 150 }
<Image src={Logo} alt="Vercel" />

Describe alternatives you've considered

N/A

Additional context

Currently, you have to manually set the width and height for a local image. Plus, the image paths aren't statically verified like an import would be.

@Timer Timer added this to the backlog milestone Oct 29, 2020
@Timer Timer changed the title Proposal: NextImage should automatically set width/height for local images Proposal: NextImage should automatically determine width/height for local images Oct 29, 2020
@neilpalima
Copy link

This is a reason why I cannot use next/image since I need to provide the width and height. It will be great if it can be used with using css width and height (ex. value of 100%) or something similar.

@agcty

This comment has been minimized.

@neilpalima

This comment has been minimized.

@agcty

This comment has been minimized.

@agcty

This comment has been minimized.

@Timer
Copy link
Member Author

Timer commented Oct 30, 2020

@neilpalima we're landing a change to <NextImage /> right now that'll allow you to specify various layouts for your image! See #18491

@Nicoowr
Copy link

Nicoowr commented Nov 10, 2020

@Timer I don't understand how #18491 is solving the issue tackled by the proposal? 😅 What I'd expect is that if one provides width=200px, <Image/> will determine height by itself? Or as a temporary solution, could I use a custom wrapper which reads the dimensions of my image, calculates the missing dimension and injects it to <Image /> ?

@gregkonush
Copy link

it would nice infer width size if the height prop was passed and the opposite way as well

@leerob
Copy link
Member

leerob commented Dec 21, 2020

One idea: we can read the width and height with the Node package image-size. I used this package to automate adding widths and heights to my images.

Maybe it's possible to use this at build time, assuming it doesn't negatively affect build time with lots of images.

@moaaz-bhnas
Copy link

moaaz-bhnas commented Jan 4, 2021

Or as a temporary solution, could I use a custom wrapper which reads the dimensions of my image, calculates the missing dimension and injects it to ?

@Nicoowr I like the idea, but won't a custom wrapper require loading the image to get its aspect ratio and do the calculation? which means loading the image twice everytime an image needs to be displayed.

@modbender
Copy link

modbender commented Jan 7, 2021

I was checking to open a issue and found this.
What I want is, supposedly I give only width, I want the height of it to be calculated automatically according to the original image width and height and vice versa. Basically I don't want to lose aspect ratio of the image. I hope this is also a feature.

@joaogarin
Copy link

Same here, it would be nice that the next image works as a normal img tag so that you can specify for example a height and by setting width auto or not set have it use the normal image ratio (like an img tag does). For now in such cases it is not possible to really use the image component =/ as seems that when you don't set either height or width you have to use layout fill which is a different use case. I do understand the potential for layout shift but we do have code that is used for different images (e.g. a logo) that we know shouldn't have more than a certain height but have the width calculated dynamically (so that it works for different logos ratios)

@7ruth
Copy link

7ruth commented Jan 18, 2021

Was struggling with keeping aspect ratios of my images, this is what I ended up using:

<div
  style={{
    height: '250px,
  }}
> 
  <div
      style={{
        position: 'relative',
        maxWidth: '100%',
        height: '100%'
      }}
   >
      <Image
          src={'./images/001'}
          alt={'picture of a sunrise'}
          layout="fill"
          objectFit="contain"
          quality={100}
      />
  </div>
</div>

@advantiot
Copy link

advantiot commented Apr 24, 2021

@7ruth Your solution is a terrific workaround, thank you. Except, I needed to add a width: '100%' as well to the outermost div otherwise the image would not show. Am I missing something?

@awareness481
Copy link
Contributor

Could this issue be closed so there's no confusion? For anyone reading this, this feature has been added in Next 11 https://nextjs.org/blog/next-11#automatic-size-detection-local-images

cc: @leerob

@vercel vercel deleted a comment from Nicoowr Jul 16, 2021
@leerob leerob closed this as completed Jul 16, 2021
@vercel vercel locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests