| 1 |
{{/* |
| 2 |
Renders the given image using the given filter, if any. |
| 3 |
|
| 4 |
When using the text filter, provide the arguments in this order: |
| 5 |
|
| 6 |
0. The text |
| 7 |
1. The horizontal offset, in pixels, relative to the left of the image (default 20) |
| 8 |
2. The vertical offset, in pixels, relative to the top of the image (default 20) |
| 9 |
3. The font size in pixels (default 64) |
| 10 |
4. The line height (default 1.2) |
| 11 |
5. The font color (default #ffffff) |
| 12 |
|
| 13 |
When using the padding filter, provide all arguments in this order: |
| 14 |
|
| 15 |
0. Padding top |
| 16 |
1. Padding right |
| 17 |
2. Padding bottom |
| 18 |
3. Padding right |
| 19 |
4. Canvas color |
| 20 |
|
| 21 |
@param {string} src The path to the image which must be a remote, page, or global resource. |
| 22 |
@param {string} [filter] The filter to apply to the image (case-insensitive). |
| 23 |
@param {string} [filterArgs] A comma-delimited list of arguments to pass to the filter. |
| 24 |
@param {bool} [example=false] If true, renders a before/after example. |
| 25 |
@param {int} [exampleWidth=384] Image width, in pixels, when rendering a before/after example. |
| 26 |
|
| 27 |
@example {{< img src="zion-national-park.jpg" >}} |
| 28 |
@example {{< img src="zion-national-park.jpg" alt="Zion National Park" >}} |
| 29 |
|
| 30 |
@example {{< img |
| 31 |
src="zion-national-park.jpg" |
| 32 |
alt="Zion National Park" |
| 33 |
filter="grayscale" |
| 34 |
>}} |
| 35 |
|
| 36 |
@example {{< img |
| 37 |
src="zion-national-park.jpg" |
| 38 |
alt="Zion National Park" |
| 39 |
filter="process" |
| 40 |
filterArgs="resize 400x webp" |
| 41 |
>}} |
| 42 |
|
| 43 |
@example {{< img |
| 44 |
src="zion-national-park.jpg" |
| 45 |
alt="Zion National Park" |
| 46 |
filter="colorize" |
| 47 |
filterArgs="180,50,20" |
| 48 |
>}} |
| 49 |
|
| 50 |
@example {{< img |
| 51 |
src="zion-national-park.jpg" |
| 52 |
alt="Zion National Park" |
| 53 |
filter="grayscale" |
| 54 |
example=true |
| 55 |
>}} |
| 56 |
|
| 57 |
@example {{< img |
| 58 |
src="zion-national-park.jpg" |
| 59 |
alt="Zion National Park" |
| 60 |
filter="grayscale" |
| 61 |
example=true |
| 62 |
exampleWidth=400 |
| 63 |
>}} |
| 64 |
|
| 65 |
@example {{< img |
| 66 |
src="images/examples/zion-national-park.jpg" |
| 67 |
alt="Zion National Park" |
| 68 |
filter="Text" |
| 69 |
filterArgs="Zion National Park,25,250,56" |
| 70 |
example=true |
| 71 |
>}} |
| 72 |
|
| 73 |
@example {{< img |
| 74 |
src="images/examples/zion-national-park.jpg" |
| 75 |
alt="Zion National Park" |
| 76 |
filter="Padding" |
| 77 |
filterArgs="20,50,20,50,#0705" |
| 78 |
example=true |
| 79 |
>}} |
| 80 |
*/}} |
| 81 |
|
| 82 |
{{- /* Initialize. */}} |
| 83 |
{{- $alt := "" }} |
| 84 |
{{- $src := "" }} |
| 85 |
{{- $filter := "" }} |
| 86 |
{{- $filterArgs := slice }} |
| 87 |
{{- $example := false }} |
| 88 |
{{- $exampleWidth := 384 }} |
| 89 |
|
| 90 |
{{- /* Default values to use with the text filter. */}} |
| 91 |
{{ $textFilterOpts := dict |
| 92 |
"xOffset" 20 |
| 93 |
"yOffset" 20 |
| 94 |
"fontSize" 64 |
| 95 |
"lineHeight" 1.2 |
| 96 |
"fontColor" "#ffffff" |
| 97 |
"fontPath" "https://github.com/google/fonts/raw/refs/heads/main/ofl/lato/Lato-Regular.ttf" |
| 98 |
}} |
| 99 |
|
| 100 |
{{- /* Get and validate parameters. */}} |
| 101 |
{{- with .Get "alt" }} |
| 102 |
{{- $alt = . }} |
| 103 |
{{- end }} |
| 104 |
|
| 105 |
{{- with .Get "src" }} |
| 106 |
{{- $src = . }} |
| 107 |
{{- else }} |
| 108 |
{{- errorf "The %q shortcode requires a file parameter. See %s" .Name .Position }} |
| 109 |
{{- end }} |
| 110 |
|
| 111 |
{{- with .Get "filter" }} |
| 112 |
{{- $filter = . | lower }} |
| 113 |
{{- end }} |
| 114 |
|
| 115 |
{{- $validFilters := slice |
| 116 |
"autoorient" "brightness" "colorbalance" "colorize" "contrast" "dither" |
| 117 |
"gamma" "gaussianblur" "grayscale" "hue" "invert" "mask" "none" "opacity" |
| 118 |
"overlay" "padding" "pixelate" "process" "saturation" "sepia" "sigmoid" "text" |
| 119 |
"unsharpmask" |
| 120 |
}} |
| 121 |
|
| 122 |
{{- with $filter }} |
| 123 |
{{- if not (in $validFilters .) }} |
| 124 |
{{- errorf "The filter passed to the %q shortcode is invalid. The filter must be one of %s. See %s" $.Name (delimit $validFilters ", " ", or ") $.Position }} |
| 125 |
{{- end }} |
| 126 |
{{- end }} |
| 127 |
|
| 128 |
{{- with .Get "filterArgs" }} |
| 129 |
{{- $filterArgs = split . "," }} |
| 130 |
{{- $filterArgs = apply $filterArgs "trim" "." " " }} |
| 131 |
{{- end }} |
| 132 |
|
| 133 |
{{- if in (slice "false" false 0) (.Get "example") }} |
| 134 |
{{- $example = false }} |
| 135 |
{{- else if in (slice "true" true 1) (.Get "example") }} |
| 136 |
{{- $example = true }} |
| 137 |
{{- end }} |
| 138 |
|
| 139 |
{{- with .Get "exampleWidth" }} |
| 140 |
{{- $exampleWidth = . | int }} |
| 141 |
{{- end }} |
| 142 |
|
| 143 |
{{- /* Get image. */}} |
| 144 |
{{- $ctx := dict "page" .Page "src" $src "name" .Name "position" .Position }} |
| 145 |
{{- $i := partial "inline/get-resource.html" $ctx }} |
| 146 |
|
| 147 |
{{- /* Resize if rendering before/after examples. */}} |
| 148 |
{{- if $example }} |
| 149 |
{{- $i = $i.Resize (printf "%dx" $exampleWidth) }} |
| 150 |
{{- end }} |
| 151 |
|
| 152 |
{{- /* Create filter. */}} |
| 153 |
{{- $f := "" }} |
| 154 |
{{- $ctx := dict "filter" $filter "args" $filterArgs "name" .Name "position" .Position }} |
| 155 |
{{- if eq $filter "autoorient" }} |
| 156 |
{{- $ctx = merge $ctx (dict "argsRequired" 0) }} |
| 157 |
{{- template "validate-arg-count" $ctx }} |
| 158 |
{{- $f = images.AutoOrient }} |
| 159 |
{{- else if eq $filter "brightness" }} |
| 160 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 161 |
{{- template "validate-arg-count" $ctx }} |
| 162 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 163 |
{{- $ctx = merge $ctx (dict "argName" "percentage" "argValue" (index $filterArgs 0) "min" -100 "max" 100) }} |
| 164 |
{{- template "validate-arg-value" $ctx }} |
| 165 |
{{- $f = images.Brightness (index $filterArgs 0) }} |
| 166 |
{{- else if eq $filter "colorbalance" }} |
| 167 |
{{- $ctx = merge $ctx (dict "argsRequired" 3) }} |
| 168 |
{{- template "validate-arg-count" $ctx }} |
| 169 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 170 |
{{- $ctx = merge $ctx (dict "argName" "percentage red" "argValue" (index $filterArgs 0) "min" -100 "max" 500) }} |
| 171 |
{{- template "validate-arg-value" $ctx }} |
| 172 |
{{- $ctx = merge $ctx (dict "argName" "percentage green" "argValue" (index $filterArgs 1) "min" -100 "max" 500) }} |
| 173 |
{{- template "validate-arg-value" $ctx }} |
| 174 |
{{- $ctx = merge $ctx (dict "argName" "percentage blue" "argValue" (index $filterArgs 2) "min" -100 "max" 500) }} |
| 175 |
{{- template "validate-arg-value" $ctx }} |
| 176 |
{{- $f = images.ColorBalance (index $filterArgs 0) (index $filterArgs 1) (index $filterArgs 2) }} |
| 177 |
{{- else if eq $filter "colorize" }} |
| 178 |
{{- $ctx = merge $ctx (dict "argsRequired" 3) }} |
| 179 |
{{- template "validate-arg-count" $ctx }} |
| 180 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 181 |
{{- $ctx = merge $ctx (dict "argName" "hue" "argValue" (index $filterArgs 0) "min" 0 "max" 360) }} |
| 182 |
{{- template "validate-arg-value" $ctx }} |
| 183 |
{{- $ctx = merge $ctx (dict "argName" "saturation" "argValue" (index $filterArgs 1) "min" 0 "max" 100) }} |
| 184 |
{{- template "validate-arg-value" $ctx }} |
| 185 |
{{- $ctx = merge $ctx (dict "argName" "percentage" "argValue" (index $filterArgs 2) "min" 0 "max" 100) }} |
| 186 |
{{- template "validate-arg-value" $ctx }} |
| 187 |
{{- $f = images.Colorize (index $filterArgs 0) (index $filterArgs 1) (index $filterArgs 2) }} |
| 188 |
{{- else if eq $filter "contrast" }} |
| 189 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 190 |
{{- template "validate-arg-count" $ctx }} |
| 191 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 192 |
{{- $ctx = merge $ctx (dict "argName" "percentage" "argValue" (index $filterArgs 0) "min" -100 "max" 100) }} |
| 193 |
{{- template "validate-arg-value" $ctx }} |
| 194 |
{{- $f = images.Contrast (index $filterArgs 0) }} |
| 195 |
{{- else if eq $filter "dither" }} |
| 196 |
{{- $f = images.Dither }} |
| 197 |
{{- else if eq $filter "gamma" }} |
| 198 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 199 |
{{- template "validate-arg-count" $ctx }} |
| 200 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 201 |
{{- $ctx = merge $ctx (dict "argName" "gamma" "argValue" (index $filterArgs 0) "min" 0 "max" 100) }} |
| 202 |
{{- template "validate-arg-value" $ctx }} |
| 203 |
{{- $f = images.Gamma (index $filterArgs 0) }} |
| 204 |
{{- else if eq $filter "gaussianblur" }} |
| 205 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 206 |
{{- template "validate-arg-count" $ctx }} |
| 207 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 208 |
{{- $ctx = merge $ctx (dict "argName" "sigma" "argValue" (index $filterArgs 0) "min" 0 "max" 1000) }} |
| 209 |
{{- template "validate-arg-value" $ctx }} |
| 210 |
{{- $f = images.GaussianBlur (index $filterArgs 0) }} |
| 211 |
{{- else if eq $filter "grayscale" }} |
| 212 |
{{- $ctx = merge $ctx (dict "argsRequired" 0) }} |
| 213 |
{{- template "validate-arg-count" $ctx }} |
| 214 |
{{- $f = images.Grayscale }} |
| 215 |
{{- else if eq $filter "hue" }} |
| 216 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 217 |
{{- template "validate-arg-count" $ctx }} |
| 218 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 219 |
{{- $ctx = merge $ctx (dict "argName" "shift" "argValue" (index $filterArgs 0) "min" -180 "max" 180) }} |
| 220 |
{{- template "validate-arg-value" $ctx }} |
| 221 |
{{- $f = images.Hue (index $filterArgs 0) }} |
| 222 |
{{- else if eq $filter "invert" }} |
| 223 |
{{- $ctx = merge $ctx (dict "argsRequired" 0) }} |
| 224 |
{{- template "validate-arg-count" $ctx }} |
| 225 |
{{- $f = images.Invert }} |
| 226 |
{{- else if eq $filter "mask" }} |
| 227 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 228 |
{{- template "validate-arg-count" $ctx }} |
| 229 |
{{- $ctx := dict "src" (index $filterArgs 0) "name" .Name "position" .Position }} |
| 230 |
{{- $maskImage := partial "inline/get-resource.html" $ctx }} |
| 231 |
{{- $f = images.Mask $maskImage }} |
| 232 |
{{- else if eq $filter "opacity" }} |
| 233 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 234 |
{{- template "validate-arg-count" $ctx }} |
| 235 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 236 |
{{- $ctx = merge $ctx (dict "argName" "opacity" "argValue" (index $filterArgs 0) "min" 0 "max" 1) }} |
| 237 |
{{- template "validate-arg-value" $ctx }} |
| 238 |
{{- $f = images.Opacity (index $filterArgs 0) }} |
| 239 |
{{- else if eq $filter "overlay" }} |
| 240 |
{{- $ctx = merge $ctx (dict "argsRequired" 3) }} |
| 241 |
{{- template "validate-arg-count" $ctx }} |
| 242 |
{{- $ctx := dict "src" (index $filterArgs 0) "name" .Name "position" .Position }} |
| 243 |
{{- $overlayImg := partial "inline/get-resource.html" $ctx }} |
| 244 |
{{- $f = images.Overlay $overlayImg (index $filterArgs 1 | float) (index $filterArgs 2 | float) }} |
| 245 |
{{- else if eq $filter "padding" }} |
| 246 |
{{- $ctx = merge $ctx (dict "argsRequired" 5) }} |
| 247 |
{{- template "validate-arg-count" $ctx }} |
| 248 |
{{- $f = images.Padding |
| 249 |
(index $filterArgs 0 | int) |
| 250 |
(index $filterArgs 1 | int) |
| 251 |
(index $filterArgs 2 | int) |
| 252 |
(index $filterArgs 3 | int) |
| 253 |
(index $filterArgs 4) |
| 254 |
}} |
| 255 |
{{- else if eq $filter "pixelate" }} |
| 256 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 257 |
{{- template "validate-arg-count" $ctx }} |
| 258 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 259 |
{{- $ctx = merge $ctx (dict "argName" "size" "argValue" (index $filterArgs 0) "min" 0 "max" 1000) }} |
| 260 |
{{- template "validate-arg-value" $ctx }} |
| 261 |
{{- $f = images.Pixelate (index $filterArgs 0) }} |
| 262 |
{{- else if eq $filter "process" }} |
| 263 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 264 |
{{- template "validate-arg-count" $ctx }} |
| 265 |
{{- $f = images.Process (index $filterArgs 0) }} |
| 266 |
{{- else if eq $filter "saturation" }} |
| 267 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 268 |
{{- template "validate-arg-count" $ctx }} |
| 269 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 270 |
{{- $ctx = merge $ctx (dict "argName" "percentage" "argValue" (index $filterArgs 0) "min" -100 "max" 500) }} |
| 271 |
{{- template "validate-arg-value" $ctx }} |
| 272 |
{{- $f = images.Saturation (index $filterArgs 0) }} |
| 273 |
{{- else if eq $filter "sepia" }} |
| 274 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 275 |
{{- template "validate-arg-count" $ctx }} |
| 276 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 277 |
{{- $ctx = merge $ctx (dict "argName" "percentage" "argValue" (index $filterArgs 0) "min" 0 "max" 100) }} |
| 278 |
{{- template "validate-arg-value" $ctx }} |
| 279 |
{{- $f = images.Sepia (index $filterArgs 0) }} |
| 280 |
{{- else if eq $filter "sigmoid" }} |
| 281 |
{{- $ctx = merge $ctx (dict "argsRequired" 2) }} |
| 282 |
{{- template "validate-arg-count" $ctx }} |
| 283 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 284 |
{{- $ctx = merge $ctx (dict "argName" "midpoint" "argValue" (index $filterArgs 0) "min" 0 "max" 1) }} |
| 285 |
{{- template "validate-arg-value" $ctx }} |
| 286 |
{{- $ctx = merge $ctx (dict "argName" "factor" "argValue" (index $filterArgs 1) "min" -10 "max" 10) }} |
| 287 |
{{- template "validate-arg-value" $ctx }} |
| 288 |
{{- $f = images.Sigmoid (index $filterArgs 0) (index $filterArgs 1) }} |
| 289 |
{{- else if eq $filter "text" }} |
| 290 |
{{- $ctx = merge $ctx (dict "argsRequired" 1) }} |
| 291 |
{{- template "validate-arg-count" $ctx }} |
| 292 |
{{- $ctx := dict "src" $textFilterOpts.fontPath "name" .Name "position" .Position }} |
| 293 |
{{- $font := or (partial "inline/get-resource.html" $ctx) }} |
| 294 |
{{- $fontSize := or (index $filterArgs 3 | int) $textFilterOpts.fontSize }} |
| 295 |
{{- $lineHeight := math.Max (or (index $filterArgs 4 | float) $textFilterOpts.lineHeight) 1 }} |
| 296 |
{{- $opts := dict |
| 297 |
"x" (or (index $filterArgs 1 | int) $textFilterOpts.xOffset) |
| 298 |
"y" (or (index $filterArgs 2 | int) $textFilterOpts.yOffset) |
| 299 |
"size" $fontSize |
| 300 |
"linespacing" (mul (sub $lineHeight 1) $fontSize) |
| 301 |
"color" (or (index $filterArgs 5) $textFilterOpts.fontColor) |
| 302 |
"font" $font |
| 303 |
}} |
| 304 |
{{- $f = images.Text (index $filterArgs 0) $opts }} |
| 305 |
{{- else if eq $filter "unsharpmask" }} |
| 306 |
{{- $ctx = merge $ctx (dict "argsRequired" 3) }} |
| 307 |
{{- template "validate-arg-count" $ctx }} |
| 308 |
{{- $filterArgs = apply $filterArgs "float" "." }} |
| 309 |
{{- $ctx = merge $ctx (dict "argName" "sigma" "argValue" (index $filterArgs 0) "min" 0 "max" 500) }} |
| 310 |
{{- template "validate-arg-value" $ctx }} |
| 311 |
{{- $ctx = merge $ctx (dict "argName" "amount" "argValue" (index $filterArgs 1) "min" 0 "max" 100) }} |
| 312 |
{{- template "validate-arg-value" $ctx }} |
| 313 |
{{- $ctx = merge $ctx (dict "argName" "threshold" "argValue" (index $filterArgs 2) "min" 0 "max" 1) }} |
| 314 |
{{- template "validate-arg-value" $ctx }} |
| 315 |
{{- $f = images.UnsharpMask (index $filterArgs 0) (index $filterArgs 1) (index $filterArgs 2) }} |
| 316 |
{{- end }} |
| 317 |
|
| 318 |
{{- /* Apply filter. */}} |
| 319 |
{{- $fi := $i }} |
| 320 |
{{- with $f }} |
| 321 |
{{- $fi = $i.Filter . }} |
| 322 |
{{- end }} |
| 323 |
|
| 324 |
{{- /* Render. */}} |
| 325 |
{{- $class := "border-1 border-gray-300 dark:border-gray-500" }} |
| 326 |
{{- if $example }} |
| 327 |
<p>Original</p> |
| 328 |
<img |
| 329 |
class="{{ $class }}" |
| 330 |
src="{{ $i.RelPermalink }}" |
| 331 |
alt="{{ $alt }}"> |
| 332 |
<p>Processed</p> |
| 333 |
<img |
| 334 |
class="{{ $class }}" |
| 335 |
src="{{ $fi.RelPermalink }}" |
| 336 |
alt="{{ $alt }}"> |
| 337 |
{{- else -}} |
| 338 |
<img |
| 339 |
src="{{ $fi.RelPermalink }}" |
| 340 |
alt="{{ $alt }}"> |
| 341 |
{{- end }} |
| 342 |
|
| 343 |
{{- define "validate-arg-count" }} |
| 344 |
{{- $msg := "When using the %q filter, the %q shortcode requires an args parameter with %d %s. See %s" }} |
| 345 |
{{- if lt (len .args) .argsRequired }} |
| 346 |
{{- $text := "values" }} |
| 347 |
{{- if eq 1 .argsRequired }} |
| 348 |
{{- $text = "value" }} |
| 349 |
{{- end }} |
| 350 |
{{- errorf $msg .filter .name .argsRequired $text .position }} |
| 351 |
{{- end }} |
| 352 |
{{- end }} |
| 353 |
|
| 354 |
{{- define "validate-arg-value" }} |
| 355 |
{{- $msg := "The %q argument passed to the %q shortcode is invalid. Expected a value in the range [%v,%v], but received %v. See %s" }} |
| 356 |
{{- if or (lt .argValue .min) (gt .argValue .max) }} |
| 357 |
{{- errorf $msg .argName .name .min .max .argValue .position }} |
| 358 |
{{- end }} |
| 359 |
{{- end }} |
| 360 |
|
| 361 |
{{- define "_partials/inline/get-resource.html" }} |
| 362 |
{{- $r := "" }} |
| 363 |
{{- $u := urls.Parse .src }} |
| 364 |
{{- $msg := "The %q shortcode was unable to resolve %s. See %s" }} |
| 365 |
{{- if $u.IsAbs }} |
| 366 |
{{- with try (resources.GetRemote $u.String) }} |
| 367 |
{{- with .Err }} |
| 368 |
{{- errorf "%s" . }} |
| 369 |
{{- else with .Value }} |
| 370 |
{{- /* This is a remote resource. */}} |
| 371 |
{{- $r = . }} |
| 372 |
{{- else }} |
| 373 |
{{- errorf $msg $.name $u.String $.position }} |
| 374 |
{{- end }} |
| 375 |
{{- end }} |
| 376 |
{{- else }} |
| 377 |
{{- with .page.Resources.Get (strings.TrimPrefix "./" $u.Path) }} |
| 378 |
{{- /* This is a page resource. */}} |
| 379 |
{{- $r = . }} |
| 380 |
{{- else }} |
| 381 |
{{- with resources.Get $u.Path }} |
| 382 |
{{- /* This is a global resource. */}} |
| 383 |
{{- $r = . }} |
| 384 |
{{- else }} |
| 385 |
{{- errorf $msg $.name $u.Path $.position }} |
| 386 |
{{- end }} |
| 387 |
{{- end }} |
| 388 |
{{- end }} |
| 389 |
{{- return $r }} |
| 390 |
{{- end -}} |