Skip to content

Commit

Permalink
Merge pull request #1055 from gethinode/develop
Browse files Browse the repository at this point in the history
Support image crop box placement
  • Loading branch information
markdumay authored Jul 29, 2024
2 parents 4728191 + 207c084 commit 1c8912e
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 12 deletions.
17 changes: 17 additions & 0 deletions data/structures/image-adapter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

19 changes: 19 additions & 0 deletions data/structures/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions exampleSite/content/en/blog/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ As an example, the following shortcode displays an image with rounded corners an
<!-- markdownlint-disable MD037 -->
{{< 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 >}}
<!-- markdownlint-enable MD037 -->

Expand All @@ -37,6 +37,6 @@ As an example, the following shortcode displays an image with rounded corners an
<!-- markdownlint-disable MD037 -->
{{< 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 >}}
<!-- markdownlint-enable MD037 -->
18 changes: 18 additions & 0 deletions layouts/partials/assets/adapters/cloudinary.html
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}}

<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "image-adapter" "args" . "group" "partial") }}
{{ errorf "partial [assets/adapter/cloudinary.html] - Invalid arguments" -}}
Expand All @@ -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 := "" }}

Expand All @@ -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 }}

Expand Down
3 changes: 2 additions & 1 deletion layouts/partials/assets/adapters/hugo.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{{ $transform := .transform }}
{{ $height := .height }}
{{ $width := .width }}
{{ $anchor := .anchor | default "" }}
{{ $element := "" }}
{{ $absoluteURL := .absoluteURL }}
{{ $url := urls.JoinPath $dir $file }}
Expand All @@ -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 }}
Expand Down
18 changes: 18 additions & 0 deletions layouts/partials/assets/adapters/imagekit.html
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}}

<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "image-adapter" "args" . "group" "partial") }}
{{ errorf "partial [assets/adapter/imagekit.html] - Invalid arguments" -}}
Expand All @@ -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 := "" }}

Expand All @@ -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 }}

Expand Down
18 changes: 18 additions & 0 deletions layouts/partials/assets/adapters/imgix.html
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}}

<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "image-adapter" "args" . "group" "partial") }}
{{ errorf "partial [assets/adapter/imgix.html] - Invalid arguments" -}}
Expand All @@ -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 := "" }}

Expand All @@ -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 }}

Expand Down
10 changes: 6 additions & 4 deletions layouts/partials/assets/helpers/image-definition.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
{{- $mode := .mode -}}
{{- $modes := .modes -}}
{{- $plain := .plain | default false }}
{{- $anchor := .anchor }}

<!-- Split url into base and anchor when applicable (only relevant for vector images) -->
{{- $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 -}}

<!-- Obtain fallback URL and imageset definition -->
Expand All @@ -34,6 +35,7 @@
"portrait" $portrait
"plain" $plain
"imageset" true
"anchor" $anchor
) }}
{{ $fallbackURL := index $target "target" }}
{{ $imgset := index $target "set" }}
Expand All @@ -53,7 +55,7 @@
{{ else }}
{{ with $wrapper }}<div class="{{ . }}">{{ end }}
{{ end }}
{{- if not $anchor -}}
{{- if not $fileAnchor -}}
<img class="img-fluid {{ $class }}"
src="{{ $fallbackURL }}"
{{ if $lazy }}loading="lazy"{{ end }}
Expand All @@ -64,7 +66,7 @@
{{ with (or $title $caption) }}alt="{{ . }}"{{ end }}>
{{- else }}
<svg class="{{ $class }}">
<use href="{{ $fallbackURL }}#{{ $anchor }}"></use>
<use href="{{ $fallbackURL }}#{{ $fileAnchor }}"></use>
</svg>
{{ end }}
{{- if $caption -}}
Expand Down
13 changes: 11 additions & 2 deletions layouts/partials/assets/helpers/image-dimension.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{{- $targetURL := "" -}}
{{- $set := "" -}}
{{- $imageset := .imageset | default false }}
{{- $anchor := .anchor | default "" -}}

<!-- Split url into base and anchor when applicable (only relevant for vector images) -->
{{- $segments := split $url "#" -}}
Expand Down Expand Up @@ -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 }}

Expand Down
2 changes: 2 additions & 0 deletions layouts/partials/assets/helpers/image-set.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -35,6 +36,7 @@
"width" $width
"height" $height
"format" $format
"anchor" $anchor
)}}
{{ if $includeWidth }}
{{ $imgset = $imgset | append (printf "%s %dw" $element $width) }}
Expand Down
2 changes: 2 additions & 0 deletions layouts/partials/assets/image.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand All @@ -49,6 +50,7 @@
"sizes" $sizes
"absoluteURL" $absoluteURL
"plain" $plain
"anchor" $anchor
)
-}}

Expand Down
3 changes: 3 additions & 0 deletions layouts/shortcodes/image.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
{{- $loading := "" -}}
{{- $error := false -}}
{{- $plain := false -}}
{{- $anchor := "" -}}

{{- if .IsNamedParams -}}
{{ $url = .Get "src" | default "" -}}
Expand All @@ -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 -}}
Expand All @@ -61,6 +63,7 @@
"mode" $mode
"portrait" $portrait
"plain" $plain
"anchor" $anchor
"loading" $loading
"page" .Page)
-}}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 1c8912e

Please sign in to comment.