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

support additional source for webp for cover/content images #766

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

provos
Copy link

@provos provos commented Jan 16, 2023

Best practices for web images is to organize them inside a picture tag and provide web optimized image formats.

Since Hugo now natively supports webp, this change wraps an image into a picture tag and provides an additional set of webp sources. The gallery css is moved from the img tag to the picture tag which required a small change to the gallery typescript.

You can check an example page that uses this at: https://www.provos.org/p/finetuning-stable-diffusion-part2/

Ps: This does not check whether the original image is webp already

@CaiJimmy
Copy link
Owner

Thank you so much for submitting your pull request!

Sorry for the late reply. Unfortunately, I'm still tied up with academic tasks and won't be able to review pull requests for the time being.

Once I have more time, I'll take a look at your pull request and provide feedback.

Thank you again for your contributions!

@CaiJimmy
Copy link
Owner

CaiJimmy commented Sep 2, 2023

Thanks for the pull request and sorry for the late response!

This is definitely a nice thing. But with this PR, each image is resized 4 times, resulting in 5 images (including the original).

I think we could just include srcset for the WebP format, since it's widely supported, and fall back to the original image in the worst case. (So only 2 resizes will be done, as it was)

What do you think?


And here's my render-image.html after making such change:

{{- $image := .Page.Resources.GetMatch (printf "%s" (.Destination | safeURL)) -}}
{{- $Permalink := .Destination | relURL | safeURL -}}
{{- $alt := .PlainText | safeHTML -}}
{{- $Width := 0 -}}
{{- $Height := 0 -}}
{{- $Srcset := "" -}}

{{/* SVG and external images won't work with gallery layout, because their width and height attributes are unknown */}}
{{- $galleryImage := false -}}

{{- if $image -}}
	{{- $notSVG := ne (path.Ext .Destination) ".svg" -}}
	{{- $Permalink = $image.RelPermalink -}}

	{{- if $notSVG -}}
		{{- $Width = $image.Width -}}
		{{- $Height = $image.Height -}}
		{{- $galleryImage = true -}}

		{{- if (default true .Page.Site.Params.imageProcessing.content.enabled) -}}
			{{- $small := $image.Resize `webp 480x` -}}
			{{- $big := $image.Resize `webp 1024x` -}}
			{{- $Srcset = printf `%s 480w, %s 1024w` $small.RelPermalink $big.RelPermalink -}}
		{{- end -}}
	{{- end -}}
{{- end -}}

<picture
	{{ if $galleryImage }}
		class="gallery-image" 
		data-flex-grow="{{ div (mul $image.Width 100) $image.Height }}"
		data-flex-basis="{{ div (mul $image.Width 240) $image.Height }}px"
	{{ end }}
>
	{{ with $Srcset }}
		<source type="image/webp"
			srcset="{{ $Srcset }}"
		>
	{{ end }}
	<img src="{{ $Permalink }}"
		{{ with $Width }}width="{{ . }}"{{ end }}
		{{ with $Height }}height="{{ . }}"{{ end }}
		loading="lazy"
		decoding="async"
		{{ with $alt }}
			alt="{{ . }}"
		{{ end }}
	>
</picture>

CaiJimmy

This comment was marked as duplicate.

@CaiJimmy
Copy link
Owner

CaiJimmy commented Sep 2, 2023

And the additional reason is that if a browser does not support WebP, it's highly probable that it does not support srcset either.

image

@provos
Copy link
Author

provos commented Oct 21, 2023 via email

@ottok
Copy link

ottok commented Feb 22, 2024

Can you @provos please rebase this on latest upstream git head? I could test it on my website to see how well it works with various browsers etc, and thus provide additional evidence that this should be merged.

PageSpeed nags on my site that webp would be better for all my images (now jpg/png) and also that I should use webm instead of animated gifs. Additionally PageSpeed does not like that all images have loading="lazy", even the ones above the fold, I this PR would also change that to decoding=async which may be better with modern browsers.

ottok added a commit to ottok/hugo-theme-stack that referenced this pull request Feb 24, 2024
@ottok
Copy link

ottok commented Feb 24, 2024

I tried testing this but mainline has diverged so much that the patch does not apply directly, and my attempts to apply it manually failed on parse failed: template: _default/_markup/render-image.html:71: unexpected EOF and I didn't figure out which of the conditionals has a mismatch..

@ottok
Copy link

ottok commented Mar 6, 2024

@CaiJimmy May I ask why you merged this on master? The point of a PR is to propose that a new branch is merged into master. Merging that proposed branch on master inside the PR seems to go against that idea. Perhaps you intended to rebase?

image

The PR is based on code from commit 4272da7 from December 2022. If it was rebased, the history would be linear. Now the history is non-linear, and the gif below shows what non-linearity means:

non-linear-history-demonstration

@CaiJimmy
Copy link
Owner

CaiJimmy commented Mar 6, 2024

@CaiJimmy May I ask why you merged this on master? The point of a PR is to propose that a new branch is merged into master. Merging that proposed branch on master inside the PR seems to go against that idea. Perhaps you intended to rebase?

image

The PR is based on code from commit 4272da7 from December 2022. If it was rebased, the history would be linear. Now the history is non-linear, and the gif below shows what non-linearity means:

non-linear-history-demonstration non-linear-history-demonstration

I did this because GitHub says "This branch is out of date with the base branch", so I clicked the button to update the branch to the latest version of the base branch. Is this a bad practice?

@CaiJimmy
Copy link
Owner

CaiJimmy commented Mar 6, 2024

also that I should use webm instead of animated gifs

@ottok I believe this is not currently supported by Hugo (gohugoio/hugo#10030). You'll have to do the transformation manually with another tool.

@provos
Copy link
Author

provos commented Mar 6, 2024

Thanks for working on this. I have unfortunately been too busy to help here.

@ottok
Copy link

ottok commented Mar 6, 2024

@CaiJimmy May I ask why you merged this on master? The point of a PR is to propose that a new branch is merged into master. Merging that proposed branch on master inside the PR seems to go against that idea. Perhaps you intended to rebase?

I did this because GitHub says "This branch is out of date with the base branch", so I clicked the button to update the branch to the latest version of the base branch. Is this a bad practice?

Does the button offer to do a rebase instead of a merge? How is your project configured? You know the concepts of linear history ("git fast-forward") and merge vs rebase, right?

image

@ottok
Copy link

ottok commented Mar 6, 2024

Thanks @provos for responding. This idea seems cool, however I can't get it working neither using the original version or any successive version, and there are too many conflicting changes so I can't get it working even when manually trying to apply the patch.

Could you rebase this on latest upstream 'master' branch and then as for re-review and testing? Thanks

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

Successfully merging this pull request may close these issues.

None yet

3 participants