-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathimage-dimension.html
More file actions
161 lines (149 loc) · 6.01 KB
/
image-dimension.html
File metadata and controls
161 lines (149 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{{/*
Copyright © 2022 - 2025 The Hinode Team / Mark Dumay. All rights reserved.
Use of this source code is governed by The MIT License (MIT) that can be found in the LICENSE file.
Visit gethinode.com/license for more details.
*/}}
{{ $error := false }}
{{/* Initialize arguments */}}
{{ $args := partial "utilities/InitArgs.html" (dict "structure" "image-dimension" "args" . "group" "partial") }}
{{ if or $args.err $args.warnmsg }}
{{ partial (cond $args.err "utilities/LogErr.html" "utilities/LogWarn.html") (dict
"partial" "assets/helpers/image-dimension.html"
"warnid" "warn-invalid-arguments"
"msg" "Invalid arguments"
"details" ($args.errmsg | append $args.warnmsg)
"file" page.File
)}}
{{ $error = $args.err }}
{{ end }}
{{/* Initialize local arguments */}}
{{- $src := $args.src -}}
{{- $height := or $args.imageHeight $args.height -}}
{{- $width := or $args.imageWidth $args.width -}}
{{- $targetURL := "" -}}
{{- $set := "" -}}
{{/* Split url into base and anchor when applicable (only relevant for vector images) */}}
{{- $segments := split $src "#" -}}
{{- if gt (len $segments) 2 -}}
{{- errorf "Invalid path or url: %q" $src -}}
{{- else if eq (len $segments) 2 }}
{{- $src = index $segments 0 -}}
{{- end -}}
{{/* Identify image provider */}}
{{ $hook := "" }}
{{ $account := "" }}
{{ $container := "" }}
{{ $rewrite := false }}
{{ range $provider, $val := site.Params.images }}
{{ if not $hook }}
{{ with index $val "host" }}
{{ if (findRE . (urls.Parse $src).Hostname) }}
{{ $hook = $provider }}
{{ with index $val "account" }}{{ $account = . }}{{ end }}
{{ with index $val "rewrite" }}{{ $rewrite = . }}{{ end }}
{{ with index $val "container" }}{{ $container = . }}{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ if not $hook }}{{ $hook = "hugo" }}{{ end }}
{{/* Rewrite the origin URL if applicable */}}
{{ if $rewrite }}
{{ $src = partial "assets/helpers/image-rewrite.html" (dict "src" $src "account" $account "container" $container "hook" $hook) }}
{{ end }}
{{/* Define image dimensions */}}
{{ $dims := slice }}
{{- $res := "" -}}
{{- $img := "" -}}
{{- $data := "" -}}
{{ $transform := "" }}
{{- if hasSuffix $src "svg" -}}
{{- $res = partial "utilities/GetResource.html" (dict "url" $src "page" $args.page) -}}
{{ if not $res }}
{{- $targetURL := partial "utilities/GetStaticURL" (dict "url" (strings.TrimPrefix "/static" $src)) -}}
{{- if not (fileExists (path.Join "/static" $targetURL)) -}}
{{ warnf "Cannot find vector image resource: %q" $src -}}
{{ else }}
{{ $width := string (partial "utilities/GetWidth.html" (dict "path" $targetURL "height" 500)) }}
{{ if $width }}
{{ $dims = $dims | append (printf "%sx500" $width) }}
{{ else }}
{{ $dims = $dims | append "500" }}
{{ end }}
{{ end }}
{{ else if eq (string $res.MediaType) "image/svg+xml" }}
{{ $data = $res.Content }}
{{ else }}
{{ warnf "Unsupported media type '%s': %q" (string $res.MediaType) $src -}}
{{ end }}
{{ else if $args.ratio }}
{{ $transform = "fill" }}
{{ $dims = partial "assets/helpers/GetDimension.html" (dict "ratio" $args.ratio) }}
{{ if not $dims }}{{ errorf "partial [assets/image.html] - Cannot find dimension data: %s" $args.ratio }}{{ end }}
{{ else if (and $height $width) }}
{{ $transform = "fill" }}
{{ $dims = slice (printf "%dx%d" $width $height) }}
{{ else if not $args.plain }}
{{ $transform = "fit" }}
{{- $res := partial "utilities/GetImage.html" (dict "url" $src "page" $args.page) -}}
{{ if and $res $res.resource }}
{{ $img = $res.resource }}
{{ if $res.mirror }}{{ $class = printf "%s mirrorred" $class }}{{ end }}
{{ $widths := partial "assets/helpers/GetDimension.html" (dict "ratio" "auto") }}
{{ range $w := $widths -}}
{{ $height = int (math.Round (mul (div (float $w) $img.Width) $img.Height)) }}
{{- $dims = $dims | append (printf "%dx%d" (int $w) $height ) -}}
{{- end -}}
{{ end }}
{{ end }}
{{/* Derive image width and height */}}
{{ if not $args.plain }}
{{ if $args.portrait }}
{{ with $dims }}
{{ $newDims := slice }}
{{ range $dim := . }}
{{ $width = (int (index (split $dim "x") 1)) }}
{{ $height = (int (index (split $dim "x") 0)) }}
{{- $newDims = $newDims | append (printf "%dx%d" (int $width) $height ) -}}
{{ end }}
{{ $dims = $newDims }}
{{ end }}
{{ $newWidth := $height }}
{{ $newHeight := $width }}
{{ $width = $newWidth }}
{{ $height = $newHeight }}
{{ else }}
{{ with $dims }}
{{ range $dim := (. | last 1) }}
{{ $width = (int (index (split $dim "x") 0)) }}
{{ $height = (int (index (split $dim "x") 1)) }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{/* Generate image url */}}
{{ if or (hasSuffix $src "svg") $args.plain }}
{{- $targetURL = partial "utilities/GetStaticURL" (dict "url" (strings.TrimPrefix "/static" $src)) -}}
{{ else }}
{{- $targetURL = partial "assets/helpers/image-set.html" (dict
"src" $src
"img" $img
"dims" ($dims | last 1)
"anchor" $args.anchor
"transform" $transform
"hook" $hook
"format" "png"
"include-width" false
)}}
{{ if $args.imageset }}
{{- $set = partial "assets/helpers/image-set.html" (dict
"src" $src
"img" $img
"dims" $dims
"anchor" $args.anchor
"transform" $transform
"hook" $hook
) -}}
{{ end }}
{{ end }}
{{ return (dict "target" $targetURL "set" $set "height" $height "width" $width "data" $data) }}