Skip to content
Grav 2.0 is officially stable. Read the announcement →
Themes & Styling

How to get the URL of the largest image in a Srcset

Solved by pamtbaau View solution

Started by Mathieu Lecouturier 5 years ago · 7 replies · 1683 views
5 years ago

Hi,
I'm looking for a way to get the Url to the largest/original image from a Srcset.
My objective would be to open it in a fullscreen lightbox using the featherlight plugin.

The code I'm using right now in the twig template is :

TWIG
{% for image in page.media.images %}
   <a href="{{ image.url() }}" rel="lightbox">{{ image.sizes('17vw').html|raw }}</a>
{% endfor %}

but image.url() returns the smallest (1x) image of the Srcset.

Would someone be able to shed some light on that matter ?
I feel like I'm missing something obvious...

Thanks!

5 years ago

Shouldn't full size be in a <a href="{FULL SIE GOES HERE}">? srcset should contain only thumbnail sizes. I mean image.url() should return unmodified version URL 🤔 Is your image really big enough?

5 years ago

@MLecout, I think the behaviour is as intended and imho correct.

The srcset attribute provides a set of alternative images to the original src image.

When using image files with density notation (@2x, @3x, ...), the @2x and up are larger alternatives for the 'original' (1x) image.

Browsers that support srcset, on devices with higher density displays, can pick a larger image size matching the density of their display.

Older browsers that don't support the srcset feature will fallback to the image referenced in the src.

See the HTML tab in the example in the docs about Higher density displays: The src attribute references the smallest alternative.

last edited 03/25/21 by pamtbaau
5 years ago

@Karmalakas, I agree, full size image should be in <a href="[email protected]">.

But, image.url return the @1x version of the picture as you can see in the code generated below :

HTML
<a href="/images/d/f/a/2/d/d-2-cqvc121x.jpeg" rel="lightbox">
    <img alt="" src="/images/d/f/a/2/d/d-2-cqvc121x.jpeg" 
    srcset="/images/b/3/0/6/4/b-0-cqvc123x.jpeg 2000w, 
    /images/0/a/3/f/9/0-0-cqvc122x.jpeg 1333w, 
    /images/d/f/a/2/d/d-2-cqvc121x.jpeg 666w" sizes="17vw">
</a>

Resulting to this kind of display on the latest version of Chrome
gz-grav|690x423

So, has you can see in the srcset, there is a @3x image available.
Then, is there a way to force image.url to return the higher density image instead of the smallest one?

5 years ago Solution

@MLecout, To get the url of the larger image, you could try the following.

Twig:

TWIG
{% for image in page.media.images %}
  <a href="{{ image.higherQualityAlternative().url() }}" rel="lightbox">
    {{ image.sizes('17vw').html|raw }}
  </a>
{% endfor %}

Generated HTML:

HTML
<a href="/images/...-image4x.jpg" rel="lightbox">
  <img alt="" 
    src="/images/...-image1x.jpg" 
    srcset="/images/...-image4x.jpg 1200w, 
            /images/...-image3x.jpg 900w, 
            /images/...-image2x.jpg 600w, 
            /images/...-image1x.jpg 300w" 
    sizes="17vw"
  >
</a>
last edited 03/25/21 by pamtbaau
5 years ago

But why wouldn't image.url() return the original? 🤔
I will have to deal with this myself, so I'm wondering if will run into the same behavior

5 years ago

@pamtbaau:
.higherQualityAlternative()

Thanks a lot!
It works just fine.

*Just wondering if I missed that method in the documentation... where did you find it?

5 years ago

@MLecout, Grav's API is also part of the documentation... ;-)

See section for class: \Grav\Common\Page\Medium\ImageMedium

👍 1

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 233 2 months ago
Themes & Styling · by Ian, 2 months ago
3 121 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 486 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 74 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 154 3 months ago