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

Generate image as background url #271

Open
1 task done
lcottingham opened this issue Aug 12, 2022 · 4 comments
Open
1 task done

Generate image as background url #271

lcottingham opened this issue Aug 12, 2022 · 4 comments

Comments

@lcottingham
Copy link

lcottingham commented Aug 12, 2022

Please provide the following information and someone from @imgix/imgix-sdk-team will respond to your issue shortly.

Before you submit:

  • Please search through the existing issues to see if your question has already been discussed.

Question
Generate a URL from the path, but set it as a background image URL? This could be a parameter specifically determining src, srcset or background-image URL.

Alternatively, could a URL be easily generated directly in a component that can be fed into a variable for HTML consumption?

It is fairly common in UI design that an image will be dynamically set as a background image so that it can fit or cover the container's available space. Yes, there is the object fit property on an image, but this isn't too widely supported on older browsers.

@sherwinski
Copy link
Contributor

sherwinski commented Aug 12, 2022

Hey @lcottingham and thanks for the question,.
You could generate a URL using https://github.com/imgix/js-core 's _buildURL and _buildSrcSet functions and pipe that into the background-image property of a component. Were you looking to implement this yourself or hoping for a separate component in this library that fits this use case?

@lcottingham
Copy link
Author

@sherwinski thanks for getting back to me 👍
Yes, very true - I could. Ideally, I was looking for a separate component in this library to fit the use case. It just could be a simple bool parameter (something like imgIxBackgroundUrl) that is added to help direct the component logic. For now, I have written a generic quick and dirty method to build the URL that I can pipe into, but it's in no way perfect.

Incorporating the js-core API methods into this package would be helpful. Yes, I could install both but i'd still have to declare two different ImgIx clients to do a very similar job. This isn't really ideal for me.

Could we look at expanding the capability slightly to recognise even just a <div> tag?

Currently we have:
<img ixImg path="examples/pione.jpg" sizes="100vw" />

What about if we have? (just like we have disableVariableQuality)
<div ixImg path="examples/pione.jpg" sizes="100vw" imgIxBackgroundUrl>...</div>

The output of this would be:
<div style="background-image:url('https://imgixsrc.net/path?qryParams...')">...</div>

@sherwinski
Copy link
Contributor

@lcottingham Totally agree that having dedicated component/logic for handling this would be most ideal. And I think that something like your suggested approach could work for those purposes.
I've made a note of this in our backlog but unfortunately can't commit to a timeline as our team is pretty heads-down working on other projects at the moment. For now I'll keep this issue open and post any updates here as they happen.

@lcottingham
Copy link
Author

Perfect - I can work with my workaround for now 👍

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

2 participants