Skip to content

Commit

Permalink
Merge pull request #940 from gethinode/image
Browse files Browse the repository at this point in the history
Add support for image CDNs
  • Loading branch information
markdumay authored May 19, 2024
2 parents 5a9191f + 59c71d9 commit ed6c415
Show file tree
Hide file tree
Showing 16 changed files with 454 additions and 237 deletions.
2 changes: 1 addition & 1 deletion config/_default/server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ for = '/**'
https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com; \
font-src 'self' https://fonts.gstatic.com; \
frame-src 'self' https://www.youtube-nocookie.com https://www.youtube.com; \
img-src 'self' data: https://i.vimeocdn.com https://i.ytimg.com https://*.google-analytics.com https://*.googletagmanager.com https://tile.openstreetmap.org; \
img-src 'self' data: https://*.cloudinary.com https://i.vimeocdn.com https://i.ytimg.com https://*.google-analytics.com https://*.googletagmanager.com https://tile.openstreetmap.org; \
manifest-src 'self'; \
media-src 'self' \
"""
Expand Down
48 changes: 48 additions & 0 deletions data/dimensions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
- ratio: 4x3
dimensions:
- 576x432
- 768x576
- 992x744
- 1200x900
- 1400x1050
- 2800x2100
- ratio: 3x2
dimensions:
- 576x384
- 768x512
- 992x661
- 1200x800
- 1400x933
- 2800x1866
- ratio: 1x1
dimensions:
- 576x576
- 768x768
- 992x992
- 1200x1200
- 1400x1400
- 2800x2800
- ratio: 16x9
dimensions:
- 576x324
- 768x432
- 992x558
- 1200x675
- 1400x788
- 2800x1576
- ratio: 21x9
dimensions:
- 576x247
- 768x329
- 992x425
- 1200x514
- 1400x600
- 2800x1200
- ratio: auto
dimensions:
- 576
- 768
- 992
- 1200
- 1400
- 2800
62 changes: 62 additions & 0 deletions data/structures/image-adapter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
comment: >-
Defines the interface to be implemented by a CDN image adapter.
arguments:
host:
type: string
optional: true
comment: >-
Host of an URL. For example, the host of the URL
'https://example.com/first/second/third.webp' equals 'example.com'.
dir:
type: string
optional: true
comment: >-
All but the last element of an URL extension. For example, the dir of the
URL 'https://example.com/first/second/third.webp' equals '/first/second/'.
file:
type: string
optional: false
comment: >-
The last element of an URL extension. For example, the file of the
URL 'https://example.com/first/second/third.webp' equals 'third.webp'.
absoluteURL:
type: string
optional: true
default: false
comment: >-
Defines if a local image should use absolute instead of relative paths.
img:
type: "*resources.resourceAdapter"
optional: true
comment: >-
Image resource to process. Must be set when handling local images.
format:
type: select
optional: true
comment: >-
Image format; leave empty for an auto format (if supported) or default
format (usually jpg).
options:
values:
- png
- jpg
- gif
- tiff
- bmp
- webp
transform:
type: select
optional: false
comment: Image transformation.
options:
values:
- fill
- fit
height:
type: int
optional: false
comment: Height of the image in pixels.
width:
type: int
optional: false
comment: Width of the image in pixels.
2 changes: 1 addition & 1 deletion exampleSite/config/_default/server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ for = '/**'
https://*.google-analytics.com https://*.analytics.google.com https://*.googletagmanager.com; \
font-src 'self' https://fonts.gstatic.com; \
frame-src 'self' https://www.youtube-nocookie.com https://www.youtube.com; \
img-src 'self' data: https://i.vimeocdn.com https://i.ytimg.com https://*.google-analytics.com https://*.googletagmanager.com https://tile.openstreetmap.org; \
img-src 'self' data: https://*.cloudinary.com https://i.vimeocdn.com https://i.ytimg.com https://*.google-analytics.com https://*.googletagmanager.com https://tile.openstreetmap.org; \
manifest-src 'self'; \
media-src 'self' \
"""
Expand Down
24 changes: 24 additions & 0 deletions exampleSite/content/en/blog/image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
author: Mark Dumay
title: Local and remote images
date: 2024-05-19
description: Include responsive images from local sources and selected CDN image providers.
tags: ["blog"]
thumbnail:
url: https://res.cloudinary.com/demo/image/upload/v1689803100/ai/hiker.jpg
author: Nathan Dumlao
authorURL: https://unsplash.com/@nate_dumlao
origin: Unsplash
originURL: https://unsplash.com/photos/QLPWQvHvmII
---

## Cloudinary

As an example, the following shortcode displays an image with rounded corners and a 21x9 aspect ratio.

<!-- markdownlint-disable MD037 -->
{{< example lang="hugo" >}}
{{</* image src="https://res.cloudinary.com/demo/image/upload/dog.webp"
ratio="21x9" caption="Cloudinary image" class="rounded" */>}}
{{< /example >}}
<!-- markdownlint-enable MD037 -->
1 change: 1 addition & 0 deletions exampleSite/hugo_stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@
"carrousel",
"carte",
"chronologie",
"cloudinary",
"collapse",
"collapse-1",
"command-prompt",
Expand Down
43 changes: 43 additions & 0 deletions layouts/partials/assets/adapters/cloudinary.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!--
Copyright © 2024 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 }}

<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "image-adapter" "args" . "group" "partial") }}
{{ errorf "partial [assets/adapter/cloudinary.html] - Invalid arguments" -}}
{{ $error = true }}
{{ end }}

<!-- Initialize arguments -->
{{ $host := .host }}
{{ $dir := .dir }}
{{ $file := .file }}
{{ $format := .format }}
{{ $transform := .transform }}
{{ $height := .height }}
{{ $width := .width }}
{{ if eq $transform "fill" }}{{ $transform = "c_fill" }}{{ else }}{{ $transform = "c_fit" }}{{ end }}
{{ $element := "" }}

<!-- Split path between upload dir and sub dir -->
{{ $newdir := urls.JoinPath (index (split $dir "upload") 0) "upload" }}
{{ $file = urls.JoinPath (index (split $dir "upload") 1) $file }}
{{ $dir = $newdir }}

<!-- Generate image URL -->
{{ if not $error }}
{{ $operation := "" }}
{{ if $format }}
{{ $operation = printf "%s,h_%d,w_%d" $transform $height $width }}
{{ $file = printf "%s.%s" (path.BaseName $file) $format }}
{{ else }}
{{ $operation = printf "f_auto,%s,h_%d,w_%d" $transform $height $width }}
{{ end }}
{{- $element = urls.JoinPath (slice "https://" $host $dir $operation $file) -}}
{{ end }}

{{ return $element }}
64 changes: 64 additions & 0 deletions layouts/partials/assets/adapters/hugo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!--
Copyright © 2024 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 }}

<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "image-adapter" "args" . "group" "partial") }}
{{ errorf "partial [assets/adapter/hugo.html] - Invalid arguments" -}}
{{ $error = true }}
{{ end }}

<!-- Initialize arguments -->
{{ $host := .host }}
{{ $dir := .dir }}
{{ $file := .file }}
{{ $img := .img }}
{{ $transform := .transform }}
{{ $height := .height }}
{{ $width := .width }}
{{ $element := "" }}
{{ $absoluteURL := .absoluteURL }}
{{ $url := urls.JoinPath $dir $file }}
{{ if $host }}
{{ $url = urls.JoinPath "https://" $host $url }}
{{ end }}

<!-- Initialize image if needed -->
{{ $format := .format | default "webp" }}
{{ if and (not $img) (ne $format "svg") }}
{{- $res := partial "utilities/GetImage.html" (dict "url" $url "page" page) -}}
{{ if $res }}
{{ $img = $res.resource }}
<!-- TODO: $res.mirror -->
{{ end }}

{{ if not $img }}
{{ errorf "partial [assets/adapter/hugo.html] - Expected 'img' argument" -}}
{{ $error = true }}
{{ end }}
{{ end }}


<!-- Process image and generate image URL -->
{{ if not $error }}
{{ $scaled := "" }}
{{ if eq $transform "fill" }}
{{- $scaled = $img.Fill (printf "%dx%d %s" $width $height $format) -}}
{{ else }}
{{- $scaled = $img.Fit (printf "%dx%d %s" $width $height $format) -}}
{{ end }}

{{- $clean := path.Ext $img.RelPermalink -}}
{{- $scaled = $scaled | resources.Copy (replace $img.RelPermalink $clean (printf "-%dx%d.%s" $width $height $format)) -}}
{{- if $absoluteURL -}}
{{- $element = $scaled.Permalink -}}
{{- else -}}
{{- $element = $scaled.RelPermalink -}}
{{- end -}}
{{ end }}

{{ return $element }}
13 changes: 13 additions & 0 deletions layouts/partials/assets/helpers/GetDimension.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!--
Copyright © 2024 The Hinode Team / Mark Dumay. All rights reserved.
-->

{{ $ratio := .ratio }}

{{ $dim := "" }}
{{ $matches := first 1 (where site.Data.dimensions "ratio" $ratio) }}
{{ if eq ($matches | len) 1 }}
{{ $dim = (index $matches 0).dimensions }}
{{ end }}

{{ return $dim }}
23 changes: 23 additions & 0 deletions layouts/partials/assets/helpers/SetPortrait.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{{- $dim := .dim -}}

{{- $dimensions := slice }}
{{- $dimensions = $dimensions | append $dim -}}
{{- $portraits := slice }}
{{- range $d := $dimensions -}}
{{- $p := split $d "x" }}
{{ if ne (len $p) 2 }}
{{- warnf "partial [assets/image.html] - Invalid dimension: %s" $d -}}
{{- $portraits = $portraits | append $d -}}
{{- else -}}
{{- $portraits = $portraits | append (printf "%sx%s" (index $p 1) (index $p 0)) -}}
{{- end -}}
{{- end -}}

{{- $ret := "" }}
{{- if reflect.IsSlice $dim -}}
{{ $ret = $portraits -}}
{{- else if gt (len $portraits) 0 -}}
{{- $ret = index $portraits 0 -}}
{{- end -}}

{{- return $ret -}}
Loading

0 comments on commit ed6c415

Please sign in to comment.