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" >}} {{* image src="https://ik.imagekit.io/demo/default-image.jpg" - ratio="21x9" caption="ImageKit.io image" class="rounded" */>}} + 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" >}} {{* image src="https://assets.imgix.net/examples/bluehat.jpg" - ratio="21x9" caption="imgix image" class="rounded" */>}} + 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 }}