hugo-scroll icon indicating copy to clipboard operation
hugo-scroll copied to clipboard

scale images on small and large screens

Open spi43984 opened this issue 2 years ago • 16 comments

I embed images by ![xxx](images/image.png).

Based on the screen size, the text is adjusted, but the image is not. On small screens it is then too large in relation to the text.

Is there some additional parameter to scale the image (like 30% of text width or something)?

spi43984 avatar Sep 29 '23 17:09 spi43984

ok, after some digging I found an approach, see https://werat.dev/blog/automatic-image-size-attributes-in-hugo/ for details.

Basically it just need a file render-image.html in layouts/_default/_markup with the following content:

{{- $image := .Page.Resources.GetMatch (printf "%s" (.Destination | safeURL)) -}}
<img
    src="{{ .Destination | safeURL }}" alt="{{ .Text }}"
    {{ with $image }}
    width="{{ $image.Width }}" height="{{ $image.Height }}"
    {{ end }}
    {{ with .Title}} style="width: 100%; max-width: {{ . }};"{{ end }}
    />

Then one need to add an image in a markdown file by ![AltText](images/image.png "50%") to scale the image to 50%.

Am still hunting for an easy way to distinguish between image size on e. g. a PC browser and a mobile device.

spi43984 avatar Sep 30 '23 13:09 spi43984

We could make it a shortcode for reuse.

zjedi avatar Sep 30 '23 21:09 zjedi

We could make it a shortcode for reuse.

I am really a rookie right now if it comes to hugo and themes. I read about shortcodes but can't really get them at the moment. I saw on hugo markdown render hooks something about render-image.html, that's why I specifically looked for that.

But whatever is cleaner and easier to maintain is good for me - my goal is to have some additional parameters to control images. There is an example on hugo image processing based on shortcodes but I haven't figured that out yet.

spi43984 avatar Oct 01 '23 09:10 spi43984

We could make it a shortcode for reuse.

Can you please advise how to proceed further?

spi43984 avatar Oct 08 '23 14:10 spi43984

@spi43984 Can you attach an image expalining the problem? I just had a look and the image is downscaled correctly in my browser.

zjedi avatar Oct 08 '23 21:10 zjedi

I took one of the pictures, removed the background and added it to the homepage. It looks huge, so I want it to scale (and not to scale the image directly to not lose quality). That's why I added the scale factor. But I am not fundamentally satisfied as with the scale factor it is scaled down the same way on large or small screens - it might make sense to use different values on different screen sizes. Like 60% on large screens and 70% on small ones. Or something more automatic like use 60% as a general value but add something in steps up to 70% if the screen size gets smaller - or whatever.

Bildschirmfoto vom 2023-10-09 11-09-58

spi43984 avatar Oct 09 '23 09:10 spi43984

I created an example (#160) - there's a section with different scaled images.

spi43984 avatar Oct 09 '23 18:10 spi43984

Just stumbled across hugo's default shortcodes - there is a figure one which can scale images too... Maybe that one is just sufficient...

{{< figure src="images/woman-pouring-juice-on-glass-3184192.jpg" width="50%">}}

spi43984 avatar Oct 14 '23 13:10 spi43984

If figure covers the issue, can we close it?

zjedi avatar Oct 14 '23 13:10 zjedi

If figure covers the issue, can we close it?

let me run some tests first... I come back to you soon.

spi43984 avatar Oct 14 '23 13:10 spi43984

I run some tests. The figure function renders per default smaller pictures - they are not column-filling. Please see the example site.

My own function creates the following html code:

<p><img src="images/happy-ethnic-woman_2000x2000_transparent.png" alt="Jane Doe" style="width: 100%; max-width: 60%;">
</p>

whereas the figure function does:

<figure><img src="images/happy-ethnic-woman_2000x2000_transparent.png" width="60%">
</figure>

What do you think - is it a drawback if the figure function doesn't render column-filling images? In that case one could just use ![Jane Doe](images/happy-ethnic-woman_2000x2000_transparent.png) instead...

spi43984 avatar Oct 15 '23 14:10 spi43984

Isn't it just matter of styling the figure with CSS? Embedding big images on single page is not ideal from performance perspective, but it's up to you :⁠-⁠)

zjedi avatar Oct 15 '23 23:10 zjedi

Isn't it just matter of styling the figure with CSS? Embedding big images on single page is not ideal from performance perspective, but it's up to you :⁠-⁠)

I wasn't concerned of the resolution (i. e. file size) but the actual rendered size on screen. Sure, that can be pretty much tweaked by CSS. As figure does what I was looking for, we can close this pull request.

spi43984 avatar Oct 16 '23 06:10 spi43984

I'll have a look at the pr still, it could be useful to have an example for other users

zjedi avatar Oct 16 '23 06:10 zjedi

<p><img src="images/happy-ethnic-woman_2000x2000_transparent.png" alt="Jane Doe" style="width: 100%; max-width: 60%;">
</p>

What sense does it make to specify width 100% and max-width 60% aren't those two contradicting?

zjedi avatar Oct 18 '23 18:10 zjedi

<p><img src="images/happy-ethnic-woman_2000x2000_transparent.png" alt="Jane Doe" style="width: 100%; max-width: 60%;">
</p>

What sense does it make to specify width 100% and max-width 60% aren't those two contradicting?

Good question - honestly I don't know. It works with or without width: 100%.

It can easily be dropped in layouts/_default/_markup/render-image.html in the line

{{ with .Title}} style="width: 100%; max-width: {{ . }};"{{ end }}

spi43984 avatar Oct 19 '23 11:10 spi43984

I'm not convinced this is to be prioritized. Images should be already scaled for optimal performance. Transferring big image and scaling it in browser seems not ideal. Closing for now, we can reopen, revisit later, if more people encounter similar needs.

zjedi avatar Mar 24 '24 22:03 zjedi

I'm ok with archiving that - but again it wasn't about transmitting large files and scale them in the client. It's about how to get the right scale of images on large and small screens. Per default on small screens images seem far too big. And as there is no fixed resolution in either case one would need scaling after all.

spi43984 avatar Mar 25 '24 07:03 spi43984