-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #940 from gethinode/image
Add support for image CDNs
- Loading branch information
Showing
16 changed files
with
454 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -492,6 +492,7 @@ | |
"carrousel", | ||
"carte", | ||
"chronologie", | ||
"cloudinary", | ||
"collapse", | ||
"collapse-1", | ||
"command-prompt", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -}} |
Oops, something went wrong.