unjs / ipx

🖼️ High performance, secure and easy-to-use image optimizer.
MIT License
2.02k stars 66 forks source link

Support `withoutEnlargement` resizing option #39

Closed ascorbic closed 3 years ago

ascorbic commented 3 years ago

It would be useful to support the withoutEnlargement option. Auto-generated srcsets often contain very large sizes, but can gracefully handle smaller sizes if those are returned instead, saving bandwidth and processor time.

This could either be a modifier in the URL or an option passed to the constructor. I'm happy to open a PR if there is a decision on the API.

pi0 commented 3 years ago

Thanks for the suggestion @ascorbic. I think it makes sense to enable withoutEnlargement resize option by default (maybe here). If needed we can support a context modifier to opt-out but seems a decent option to be enabled by default.

ascorbic commented 3 years ago

Yes, I agree. I think in the image proxy context it's reasonable to make that the default. Maybe a modifier enlarge if it's no longer the default?

ascorbic commented 3 years ago

I've opened a PR what will fix this. When both dimensions are set, sharp doesn't behave as expected for withoutEnlargement (it doesn't preserve the requested aspect ratio), so I implemented the limits myself instead.