Created
April 23, 2021 14:31
-
-
Save adityatelange/376cd56ee2c94aaa2e8b93200f2ba8b5 to your computer and use it in GitHub Desktop.
Code snippet used in https://adityatelange.in/blog/hugo-watermarking-images/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of render-image.html --> | |
{{- $link := split .Destination "#" }} | |
{{- $image := (.Page.Resources.ByType "image").GetMatch (printf "*%s*" (index $link 0)) }} | |
{{- $logo := (resources.Get "images/logo.png") }} | |
{{- if and $image $logo }} | |
{{- $size := math.Round (mul $image.Height 0.25) }} | |
{{- $size := cond (ge $size 80) ($size) (80.0) }} | |
{{- $logo := $logo.Resize (printf "%.0fx jpg" $size) }} | |
{{- $image := $image.Filter (images.Overlay $logo (sub $image.Width $logo.Width) (sub $image.Height $logo.Height) ) }} | |
{{- $finalUrl := cond (isset $link 1) (printf "%s#%s" ($image.Permalink) (index $link 1)) ($image.Permalink) -}} | |
<img src="{{ $finalUrl | safeURL }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}" {{ end }} /> | |
{{- else }} | |
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}" {{ end }} /> | |
{{- end }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment