From 8c96d179a494348e022a85dfca0db756b926fad1 Mon Sep 17 00:00:00 2001 From: GerkinDev Date: Sun, 16 Jan 2022 17:28:42 +0100 Subject: [PATCH] perf: optimize avatar from assets --- layouts/partials/body/whoami.html | 23 ++++------------ layouts/partials/sidebar/site-bio.html | 30 ++++++-------------- layouts/partials/whoami/img-url.html | 38 ++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 layouts/partials/whoami/img-url.html diff --git a/layouts/partials/body/whoami.html b/layouts/partials/body/whoami.html index 2428dcd..83df0aa 100644 --- a/layouts/partials/body/whoami.html +++ b/layouts/partials/body/whoami.html @@ -3,23 +3,12 @@
- {{ $src := "" }} - {{ if site.Params.useGravatar }} - {{ $src = printf "https://s.gravatar.com/avatar/%s?s=200" (md5 ($.Param "email")) }} - {{ if $.Param - {{ else }} - {{ if $.Param "authorImageUrl" }} - {{ if $.Param - {{ else if $.Param "authorImage" }} - {{ if $.Param - {{ else if (fileExists "static/images/whoami/avatar.png") }} - {{ if $.Param - {{ else if (fileExists "static/images/whoami/avatar.svg") }} - {{ if $.Param - {{ else }} - {{ if $.Param - {{ end }} - {{ end }} + {{ $url := partial "partials/whoami/img-url" (dict + "urlSource" ($.Param "authorImageUrl") + "fileSource" ($.Param "authorImage") + "size" 150 + )}} + {{ if $.Param
diff --git a/layouts/partials/sidebar/site-bio.html b/layouts/partials/sidebar/site-bio.html index 57fc4b5..19db697 100644 --- a/layouts/partials/sidebar/site-bio.html +++ b/layouts/partials/sidebar/site-bio.html @@ -3,23 +3,11 @@ {{ if ($.Param "enableBioImage" | default true) }}
- {{ $src := "" }} - {{ if site.Params.useGravatar }} - {{ $src = printf "https://s.gravatar.com/avatar/%s?s=200" (md5 ($.Param "email")) }} - {{ if $.Param - {{ else }} - {{ if $.Param "bioImageUrl" }} - {{ if $.Param - {{ else if (fileExists "static/images/whoami/avatar.png") }} - {{ if $.Param - {{ else if (fileExists "static/images/whoami/avatar.jpg") }} - {{ if $.Param - {{ else if (fileExists "static/images/whoami/avatar.svg") }} - {{ if $.Param - {{ else }} - {{ if $.Param - {{ end }} - {{ end }} + {{ $url := partial "partials/whoami/img-url" (dict + "urlSource" ($.Param "bioImageUrl") + "size" 250 + )}} + {{ if $.Param
{{ end }}
@@ -29,7 +17,7 @@
{{ $.Param "whoami" }}
- + {{ if $.Param "organization" }}
@@ -50,10 +38,10 @@ {{ if $.Param "email" }}
-
+
{{ partial "svgs/social/email.svg" (dict "width" 20 "height" 20) }}
- {{ $.Param "email" }} + {{ $.Param "email" }}
{{ end }} @@ -62,7 +50,7 @@
{{ partial "svgs/etc/link.svg" (dict "width" 20 "height" 20) }}
- {{ $.Param "link" }} + {{ $.Param "link" }}
{{ end }} diff --git a/layouts/partials/whoami/img-url.html b/layouts/partials/whoami/img-url.html new file mode 100644 index 0000000..1540c65 --- /dev/null +++ b/layouts/partials/whoami/img-url.html @@ -0,0 +1,38 @@ + +{{- if site.Params.useGravatar -}} + {{- printf "https://s.gravatar.com/avatar/%s?s=%d" (md5 .email) .size -}} +{{- else if .urlSoure -}} {{/* If an url source is set, use it directly. */}} + {{- .urlSoure -}} +{{- else -}} + {{- $imageResource := "" -}} + + {{- if .fileSource -}} + {{- if (hasPrefix .fileSource "assets/") -}} {{/* If the explicit file source is within assets, get its resource */}} + {{- $imageResource = resources.Get (strings.TrimPrefix "assets/" .fileSource) -}} + {{- end -}} + {{- else -}} {{/* If file source is not explicitly set, try to find an avatar */}} + {{- if (fileExists "assets/images/whoami/avatar.png") -}} + {{- $imageResource = resources.Get "images/whoami/avatar.png" -}} + {{- else if (fileExists "assets/images/whoami/avatar.jpg") -}} + {{- $imageResource = resources.Get "images/whoami/avatar.jpg" -}} + {{- else if (fileExists "assets/images/whoami/avatar.svg") -}} + {{- $imageResource = resources.Get "images/whoami/avatar.svg" -}} + {{- end -}} + {{- end -}} + + {{- if $imageResource -}} {{/* If we have an asset image, resize it & get its url */}} + {{- ($imageResource.Resize (printf "%dx" .size)).RelPermalink -}} + {{- else if .fileSource -}} {{/* If a file source was specified but it did not resulted in an asset, it's a static file */}} + {{- .fileSource -}} + {{- else -}} {{/* Otherwise, lookup in static files */}} + {{- if (fileExists "static/images/whoami/avatar.png") -}} + {{- "images/whoami/avatar.png" | relURL -}} + {{- else if (fileExists "static/images/whoami/avatar.jpg") -}} + {{- "images/whoami/avatar.jpg" | relURL -}} + {{- else if (fileExists "static/images/whoami/avatar.svg") -}} + {{- "images/whoami/avatar.svg" | relURL -}} + {{- else -}} + {{- "images/whoami/avatar.jpg" | relURL -}} + {{- end -}} + {{- end -}} +{{- end -}} \ No newline at end of file