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

[Feature Request] Include generated image dimensions #10

Closed
bensmithett opened this issue Aug 6, 2022 · 2 comments · Fixed by #18
Closed

[Feature Request] Include generated image dimensions #10

bensmithett opened this issue Aug 6, 2022 · 2 comments · Fixed by #18
Labels
enhancement New feature or request

Comments

@bensmithett
Copy link

Taking the example from the readme...

widthPreset({
  class: 'img thumb',
  loading: 'lazy',
  widths: [48, 96],
  formats: {
    webp: { quality: 50 },
    jpg: { quality: 70 }
  }
})

it would be great to have a width and height attributes to spread onto the <img> in order to give it the correct intrinsic dimensions and reserve the correct amount of space when loading to avoid layout shifts, e.g. assuming a 100x150 source image

expect(thumbnails).toEqual([
  {
    type: 'image/webp',
    srcset: '/assets/logo.ffc730c4.webp 48w, /assets/logo.1f874174.webp 96w',
  },
  {
    type: 'image/jpeg',
    srcset: '/assets/logo.063759b1.jpeg 48w, /assets/logo.81d93491.jpeg 96w',
    src: '/assets/logo.81d93491.jpeg',
    class: 'img thumb',
    loading: 'lazy',
    
    // width and height of the generated image used for `src`
    width: 96,
    height: 144,
  },
])
@ElMassimo
Copy link
Owner

Hi Ben!

For some presets (like the density one) and certain use cases the size of the image does not represent the display size.

I would make this feature opt-in, probably by passing an option to the helper when defining a preset.

PRs are welcome!

@ElMassimo ElMassimo added the enhancement New feature or request label Aug 9, 2022
@bensmithett
Copy link
Author

Opt-in makes sense! I did take a look to see if I could quickly PR something, but IIUC the sharp image object is only accessed as part of srcset generation so it might be a reasonably big change

generate: async (image, args) => {

I'll take a closer look when I get a chance! Thanks for the excellent plugin 🌟

(FWIW this feature comes with https://github.com/dazuaz/responsive-loader which is what I'm migrating from)

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

Successfully merging a pull request may close this issue.

2 participants