diff --git a/data/structures/image-adapter.yml b/data/structures/image-adapter.yml index 74522c776..e937fe820 100644 --- a/data/structures/image-adapter.yml +++ b/data/structures/image-adapter.yml @@ -60,3 +60,20 @@ arguments: type: int optional: false comment: Width of the image in pixels. + anchor: + type: select + optional: true + comment: Anchor of the crop box. + options: + values: + - TopLeft + - Top + - TopRight + - Left + - Center + - Right + - BottomLeft + - Bottom + - BottomRight + - Smart + diff --git a/data/structures/image.yml b/data/structures/image.yml index e1623ffb6..6bc2024b4 100644 --- a/data/structures/image.yml +++ b/data/structures/image.yml @@ -138,3 +138,22 @@ arguments: Flag to indicate if the image should render a plain image instead of an image set. When set, no transformations are applied to the image. release: v0.24.0 + anchor: + type: select + optional: true + comment: + Anchor of the crop box, defaults to anchor value set in `imaging` section + of the site configuration (usually `Smart`). + options: + values: + - TopLeft + - Top + - TopRight + - Left + - Center + - Right + - BottomLeft + - Bottom + - BottomRight + - Smart + release: v0.24.22 diff --git a/exampleSite/content/en/blog/image.md b/exampleSite/content/en/blog/image.md index 24892e634..7fa7cc71e 100644 --- a/exampleSite/content/en/blog/image.md +++ b/exampleSite/content/en/blog/image.md @@ -26,7 +26,7 @@ As an example, the following shortcode displays an image with rounded corners an {{< example lang="hugo" >}} {{}} + ratio="21x9" caption="ImageKit.io image" class="rounded" anchor="Center" */>}} {{< /example >}} @@ -37,6 +37,6 @@ As an example, the following shortcode displays an image with rounded corners an {{< example lang="hugo" >}} {{}} + ratio="21x9" caption="imgix image" class="rounded" anchor="Top" */>}} {{< /example >}} diff --git a/layouts/partials/assets/adapters/cloudinary.html b/layouts/partials/assets/adapters/cloudinary.html index 78624c0ba..431b03849 100644 --- a/layouts/partials/assets/adapters/cloudinary.html +++ b/layouts/partials/assets/adapters/cloudinary.html @@ -6,6 +6,19 @@ {{ $error := false }} +{{ $anchorMap := dict + "TopLeft" "north_west" + "Top" "north" + "TopRight" "north_east" + "Left" "west" + "Center" "center" + "Right" "east" + "BottomLeft" "south_west" + "Bottom" "south" + "BottomRight" "south_east" + "Smart" "auto" +}} + {{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "image-adapter" "args" . "group" "partial") }} {{ errorf "partial [assets/adapter/cloudinary.html] - Invalid arguments" -}} @@ -20,6 +33,8 @@ {{ $transform := .transform }} {{ $height := .height }} {{ $width := .width }} +{{ $anchor := "" }} +{{ with .anchor }}{{ $anchor = index $anchorMap . }}{{ end }} {{ if eq $transform "fill" }}{{ $transform = "c_fill" }}{{ else }}{{ $transform = "c_fit" }}{{ end }} {{ $element := "" }} @@ -37,6 +52,9 @@ {{ else }} {{ $operation = printf "f_auto,%s,h_%d,w_%d" $transform $height $width }} {{ end }} + {{ with $anchor }} + {{ $operation = printf "%s,g_%s" $operation . }} + {{ end }} {{- $element = urls.JoinPath (slice "https://" $host $dir $operation $file) -}} {{ end }} diff --git a/layouts/partials/assets/adapters/hugo.html b/layouts/partials/assets/adapters/hugo.html index a4ba0aa70..b0876c9a4 100644 --- a/layouts/partials/assets/adapters/hugo.html +++ b/layouts/partials/assets/adapters/hugo.html @@ -20,6 +20,7 @@ {{ $transform := .transform }} {{ $height := .height }} {{ $width := .width }} +{{ $anchor := .anchor | default "" }} {{ $element := "" }} {{ $absoluteURL := .absoluteURL }} {{ $url := urls.JoinPath $dir $file }} @@ -46,7 +47,7 @@ {{ if not $error }} {{ $scaled := "" }} {{ if eq $transform "fill" }} - {{- $scaled = $img.Fill (printf "%dx%d %s" $width $height $format) -}} + {{- $scaled = $img.Fill (printf "%dx%d %s %s" $width $height $anchor $format) -}} {{ else }} {{- $scaled = $img.Fit (printf "%dx%d %s" $width $height $format) -}} {{ end }} diff --git a/layouts/partials/assets/adapters/imagekit.html b/layouts/partials/assets/adapters/imagekit.html index 5685b0eca..9fb0ff2c2 100644 --- a/layouts/partials/assets/adapters/imagekit.html +++ b/layouts/partials/assets/adapters/imagekit.html @@ -6,6 +6,19 @@ {{ $error := false }} +{{ $anchorMap := dict + "TopLeft" "top_left" + "Top" "top" + "TopRight" "top_right" + "Left" "left" + "Center" "center" + "Right" "right" + "BottomLeft" "bottom_left" + "Bottom" "bottom" + "BottomRight" "bottom_right" + "Smart" "auto" +}} + {{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "image-adapter" "args" . "group" "partial") }} {{ errorf "partial [assets/adapter/imagekit.html] - Invalid arguments" -}} @@ -20,6 +33,8 @@ {{ $transform := .transform }} {{ $height := .height }} {{ $width := .width }} +{{ $anchor := "" }} +{{ with .anchor }}{{ $anchor = index $anchorMap . }}{{ end }} {{ if eq $transform "fill" }}{{ $transform = "c-maintain_ratio" }}{{ else }}{{ $transform = "c-at_max" }}{{ end }} {{ $element := "" }} @@ -42,6 +57,9 @@ {{ else }} {{ $operation = printf "tr:f-auto,%s,h-%d,w-%d" $transform $height $width }} {{ end }} + {{ with $anchor }} + {{ $operation = printf "%s,fo-%s" $operation . }} + {{ end }} {{- $element = urls.JoinPath (slice "https://" $host $dir $operation $file) -}} {{ end }} diff --git a/layouts/partials/assets/adapters/imgix.html b/layouts/partials/assets/adapters/imgix.html index 823a4dc4f..7b0350469 100644 --- a/layouts/partials/assets/adapters/imgix.html +++ b/layouts/partials/assets/adapters/imgix.html @@ -6,6 +6,19 @@ {{ $error := false }} +{{ $anchorMap := dict + "TopLeft" "top,left" + "Top" "top" + "TopRight" "top,right" + "Left" "left" + "Center" "center" + "Right" "right" + "BottomLeft" "bottom,left" + "Bottom" "bottom" + "BottomRight" "bottom,right" + "Smart" "faces,edges,center" +}} + {{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "image-adapter" "args" . "group" "partial") }} {{ errorf "partial [assets/adapter/imgix.html] - Invalid arguments" -}} @@ -20,6 +33,8 @@ {{ $transform := .transform }} {{ $height := .height }} {{ $width := .width }} +{{ $anchor := "" }} +{{ with .anchor }}{{ $anchor = index $anchorMap . }}{{ end }} {{ if eq $transform "fill" }}{{ $transform = "crop" }}{{ else }}{{ $transform = "max" }}{{ end }} {{ $element := "" }} @@ -37,6 +52,9 @@ {{ else }} {{ $operation = printf "f_auto&fit=%s&h=%d&w=%d" $transform $height $width }} {{ end }} + {{ with $anchor }} + {{ $operation = printf "%s&crop=%s" $operation . }} + {{ end }} {{- $element = printf "%s?%s" (urls.JoinPath (slice "https://" $host $dir $file)) $operation -}} {{ end }} diff --git a/layouts/partials/assets/helpers/image-definition.html b/layouts/partials/assets/helpers/image-definition.html index 210074b46..e2a070b38 100644 --- a/layouts/partials/assets/helpers/image-definition.html +++ b/layouts/partials/assets/helpers/image-definition.html @@ -15,15 +15,16 @@ {{- $mode := .mode -}} {{- $modes := .modes -}} {{- $plain := .plain | default false }} +{{- $anchor := .anchor }} -{{- $anchor := "" -}} +{{- $fileAnchor := "" -}} {{- $segments := split $url "#" -}} {{- if gt (len $segments) 2 -}} {{- errorf "Invalid path or url: %q" $url -}} {{- else if eq (len $segments) 2 }} {{- $url = index $segments 0 -}} - {{- $anchor = index $segments 1 -}} + {{- $fileAnchor = index $segments 1 -}} {{- end -}} @@ -34,6 +35,7 @@ "portrait" $portrait "plain" $plain "imageset" true + "anchor" $anchor ) }} {{ $fallbackURL := index $target "target" }} {{ $imgset := index $target "set" }} @@ -53,7 +55,7 @@ {{ else }} {{ with $wrapper }}
{{ end }} {{ end }} -{{- if not $anchor -}} +{{- if not $fileAnchor -}} {{- else }} - + {{ end }} {{- if $caption -}} diff --git a/layouts/partials/assets/helpers/image-dimension.html b/layouts/partials/assets/helpers/image-dimension.html index 5ae851413..2fb912259 100644 --- a/layouts/partials/assets/helpers/image-dimension.html +++ b/layouts/partials/assets/helpers/image-dimension.html @@ -9,6 +9,7 @@ {{- $targetURL := "" -}} {{- $set := "" -}} {{- $imageset := .imageset | default false }} +{{- $anchor := .anchor | default "" -}} {{- $segments := split $url "#" -}} @@ -118,13 +119,21 @@ "url" $url "img" $img "dims" ($dims | last 1) + "anchor" $anchor "transform" $transform "hook" $hook - "format" "jpg" + "format" "png" "includeWidth" false )}} {{ if $imageset }} - {{- $set = partial "assets/helpers/image-set.html" (dict "url" $url "img" $img "dims" $dims "transform" $transform "hook" $hook) -}} + {{- $set = partial "assets/helpers/image-set.html" (dict + "url" $url + "img" $img + "dims" $dims + "anchor" $anchor + "transform" $transform + "hook" $hook + ) -}} {{ end }} {{ end }} diff --git a/layouts/partials/assets/helpers/image-set.html b/layouts/partials/assets/helpers/image-set.html index b383ca1d5..ce381fc2b 100644 --- a/layouts/partials/assets/helpers/image-set.html +++ b/layouts/partials/assets/helpers/image-set.html @@ -7,6 +7,7 @@ {{ $transform := .transform }} {{ $format := .format }} {{ $includeWidth := .includeWidth | default true }} +{{ $anchor := .anchor | default "" }} {{ $host := (urls.Parse $url).Hostname }} {{ $dir := (urls.Parse $url).Path }} @@ -35,6 +36,7 @@ "width" $width "height" $height "format" $format + "anchor" $anchor )}} {{ if $includeWidth }} {{ $imgset = $imgset | append (printf "%s %dw" $element $width) }} diff --git a/layouts/partials/assets/image.html b/layouts/partials/assets/image.html index 1e7244206..37a70f17f 100644 --- a/layouts/partials/assets/image.html +++ b/layouts/partials/assets/image.html @@ -27,6 +27,7 @@ {{- if eq $priority "auto" }}{{ $priority = "" }}{{ end -}} {{- $sizes := .sizes | default "100vw" -}} {{- $plain := .plain | default false -}} +{{- $anchor := .anchor | default "" -}} {{- $absoluteURL := site.Params.main.canonifyAssetsURLs | default false -}} {{- $url := .url -}} @@ -49,6 +50,7 @@ "sizes" $sizes "absoluteURL" $absoluteURL "plain" $plain + "anchor" $anchor ) -}} diff --git a/layouts/shortcodes/image.html b/layouts/shortcodes/image.html index 47b5841fd..f8b840648 100644 --- a/layouts/shortcodes/image.html +++ b/layouts/shortcodes/image.html @@ -25,6 +25,7 @@ {{- $loading := "" -}} {{- $error := false -}} {{- $plain := false -}} +{{- $anchor := "" -}} {{- if .IsNamedParams -}} {{ $url = .Get "src" | default "" -}} @@ -39,6 +40,7 @@ {{ with .Get "mode" }}{{ $mode = partial "utilities/CastBool.html" . }}{{ end -}} {{ with .Get "portrait" }}{{ $portrait = partial "utilities/CastBool.html" . }}{{ end -}} {{ with .Get "plain" }}{{ $plain = partial "utilities/CastBool.html" . }}{{ end -}} + {{ $anchor = .Get "anchor" | default "" -}} {{ else -}} {{ $url = .Get 0 }} {{ end -}} @@ -61,6 +63,7 @@ "mode" $mode "portrait" $portrait "plain" $plain + "anchor" $anchor "loading" $loading "page" .Page) -}} diff --git a/package-lock.json b/package-lock.json index def04b361..41d08d705 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@gethinode/hinode", - "version": "0.24.21", + "version": "0.24.22", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@gethinode/hinode", - "version": "0.24.21", + "version": "0.24.22", "license": "MIT", "dependencies": { "@fullhuman/postcss-purgecss": "^6.0.0", diff --git a/package.json b/package.json index b7f96a59a..8a38b77bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gethinode/hinode", - "version": "0.24.21", + "version": "0.24.22", "description": "Hinode is a clean documentation and blog theme for Hugo, an open-source static site generator", "keywords": [ "hugo",