blog/themes/stack/layouts/_partials/helper/image.html
2026-07-31 15:38:57 +08:00

72 lines
2.1 KiB
HTML

/// Params:
/// Resources: Where to search for images
/// Image: Image URL
/// Context: page context (used to access the configuration)
/// Returns:
/// Resource: Hugo image resource object
/// Permalink: Image URL
/// Height: Image height
/// Width: Image width
{{ $result := dict }}
{{ if .Image }}
{{ $url := urls.Parse .Image }}
{{ $permalink := .Image }}
{{ $result = dict
"Resource" nil
"Permalink" $permalink
"Height" nil
"Width" nil
}}
{{ $resource := dict }}
{{ $local := true }}
{{ if not (in (slice "https" "http") $url.Scheme) }}
{{/* Local image */}}
{{ $resource = .Resources.Get (printf "%s" (.Image | safeURL)) }}
{{ else }}
{{/* Remote image */}}
{{ with try (resources.GetRemote $url (dict "timeout" .Context.Site.Params.imageProcessing.external.timeout)) }}
{{ with .Err }}
{{ warnf "Failed to fetch remote resource %q: %s" $url . }}
{{ else with .Value }}
{{ $resource = . }}
{{ $local = false }}
{{ else }}
{{ warnf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
{{ end }}
{{ if $resource }}
{{ if and .Context.Site.Params.imageProcessing.autoOrient (reflect.IsImageResourceProcessable $resource) }}
{{ $filter := images.AutoOrient }}
{{ $resource = $resource | images.Filter $filter }}
{{ end }}
{{ if $local }}
{{ $permalink = $resource.RelPermalink }}
{{ end }}
{{ if reflect.IsImageResourceWithMeta $resource }}
{{ $result = dict
"Resource" $resource
"Permalink" $permalink
"Height" $resource.Height
"Width" $resource.Width
}}
{{ else }}
{{ $result = dict
"Resource" $resource
"Permalink" $permalink
"Height" nil
"Width" nil
}}
{{ end }}
{{ end }}
{{ end }}
{{ return $result }}