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

Add option to guess and calculate width and height for an image format #36

Merged

Conversation

alexander-schranz
Copy link
Member

@alexander-schranz alexander-schranz commented Sep 17, 2021

Set width and height attribute automatically

Since Sulu 2.3 the original image width and height are save as properties.
This allows us the guess the width and height dimension of a given image format.

With setting the width and height attribute modern browsers avoid a layer shifts
and the page will not jump when images are loaded.

This feature can be activated the following way:

services:
    Sulu\Twig\Extensions\ImageExtension:
        arguments:
             $aspectRatio: true
             $imageFormatConfiguration: '%sulu_media.image.formats%'

So if we have an original image of 1920x1080 and have a image format called 100x:

{{ get_image(headerImage, '100x') }}

Will add automatically the width and height attributes to the imgage tag:

<img alt="Title" title="Description" src="/uploads/media/100x/01/image.jpg?v=1-0" width="100" height="56">

This only works when writing image formats in specific format 100x, x100, 100x100, 100x100-inset, 100x@2x, ...
for all other cases this option not be activated.

fixes #36

@alexander-schranz alexander-schranz force-pushed the feature/image-guess-width-height branch 2 times, most recently from c22ef01 to f0e7320 Compare September 17, 2021 09:03
@alexander-schranz alexander-schranz force-pushed the feature/image-guess-width-height branch 2 times, most recently from ff22987 to 43c9601 Compare September 17, 2021 09:19
docs/image.md Outdated Show resolved Hide resolved
docs/image.md Outdated Show resolved Hide resolved
docs/image.md Outdated Show resolved Hide resolved
docs/image.md Outdated Show resolved Hide resolved
docs/image.md Outdated Show resolved Hide resolved
docs/image.md Outdated Show resolved Hide resolved
* 1: int|null,
* }
*/
private function guessAspectRatio($media, array $attributes): array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accessing the %sulu_media.image.formats% parameter would allow us to not rely on the format key here. but i understand that it makes the method dependent on sulu.

another possibility would be trying to access the %sulu_media.image.formats% parameter and fallback to this implementation if it is not available.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont have a lot of experience with this package and i am not sure if anything sulu related is accessed somewhere else. if yes, i think it would be nice to use %sulu_media.image.formats%. if not, we should probably not start doing it 🙂

Copy link
Member Author

@alexander-schranz alexander-schranz Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the web twig extension has no sulu dependencies and so are sulu independent. As sulu_media.image.formats is no service just an array configuration which we could reuse without a dependency to sulu.

I'm fine to add imageFormatsConfiguration as parameter and use it when is configured and else fallback to it. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it would be nice to do that, because the feature would not depend on the format of the keys then 🙂

Copy link
Member Author

@alexander-schranz alexander-schranz Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did update the extension!

@alexander-schranz alexander-schranz force-pushed the feature/image-guess-width-height branch 4 times, most recently from b602dc1 to e109c5c Compare September 17, 2021 11:59
docs/image.md Outdated Show resolved Hide resolved
Co-authored-by: nnatter <niklas.natter@gmail.com>
@niklasnatter niklasnatter merged commit d1da80d into sulu:master Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants