-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctflPicture.js
130 lines (110 loc) · 4.11 KB
/
ctflPicture.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ==========================================================
This shortcode can be used for images loaded from Contentful.
It generates a <picture> with all the formats and widths defined in the shortcode.
Required properties:
- imgObj -> The whole image object from contentful (not just the URL!)
Not required, but recommended:
- alt -> alt for the image (works with ""). Defaults to the image's title defined in Contentful
- imgWidth -> The width of the image requested from contentful. Defaults to 800
- imgHeight -> The height of the image requested from contentful. Defaults to 600
!! if you define either imgWidth or imgHeight, the other one will be calculated based on the image's
dimensions taken from contentful. If you specify both, the image will be resized. !!
Optional properties:
- formats -> The image formats generated for the picture. Defaults to ["avif", "webp", "jpg"]
- widths -> all the widths for the picture elements. Works with [null]. Defaults to [300, 600]
- sizes -> defines the sizes for the picture. Defaults to "(min-width: 22em) 30vw, 100vw"
- classes -> add some classes
- fit -> if you resize the image this defines how it should be resized. Defaults to "fill"
Basic usage:
{% ctflPicture imgObj = myImage, alt="ctfl image", imgWidth="800", imgHeight="600" %}
Thumbnail example:
{% ctflPicture
imgObj = myImage,
alt="Avatar",
imgWidth="32",
imgHeight="32",
fit="thumb",
widths=[32],
sizes="2rem"
%}
========================================================== */
const eleventyImage = require("@11ty/eleventy-img");
async function ctflPictureShortcode(ctflImage) {
if (!ctflImage.imgObj) {
return "Contentful image-object must be provided";
}
if (
ctflImage.imgObj.fields == undefined ||
ctflImage.imgObj.fields.file == undefined ||
ctflImage.imgObj.fields.file.contentType == undefined ||
ctflImage.imgObj.fields.file.contentType.startsWith("image") == false
) {
return "imgObj must be a valid contentful image object";
}
const { imgObj } = ctflImage;
let imgUrl = imgObj.fields.file.url;
const imgId = imgObj.sys.id;
const originSizes = imgObj.fields.file.details.image;
const ratio = originSizes.width / originSizes.height;
let alt = '';
if (ctflImage.alt == undefined) {
alt = imgObj.fields.title.replace(/["']/g, '"');
} else {
alt = ctflImage.alt.replace(/["']/g, '"');
}
const formats = ctflImage.formats || ["avif", "webp", "jpg"];
const widths = ctflImage.widths || [300, 600];
const sizes = ctflImage.sizes || "(min-width: 22em) 30vw, 100vw";
const classes = ctflImage.classes || "";
const fit = ctflImage.fit ? ctflImage.fit : "fill";
const focus = ctflImage.focus ? ctflImage.focus : "center";
let imgWidth = 800;
let imgHeight = 600;
if (ctflImage.imgWidth && ctflImage.imgHeight) {
imgWidth = ctflImage.imgWidth;
imgHeight = ctflImage.imgHeight;
}
if (ctflImage.imgWidth && !ctflImage.imgHeight) {
let calculatedHeight = Math.floor(ctflImage.imgWidth / ratio);
imgWidth = ctflImage.imgWidth;
imgHeight = calculatedHeight;
}
if (!ctflImage.imgWidth && ctflImage.imgHeight) {
let calculatedWidth = Math.floor(ctflImage.imgHeight * ratio);
imgHeight = ctflImage.imgHeight;
imgWidth = calculatedWidth;
}
if (imgUrl.startsWith("//")) {
imgUrl = "https:" + imgUrl;
}
imgUrl = imgUrl + "?f=" + focus + "&fit=" + fit + "&w=" + imgWidth + "&h=" + imgHeight;
let options;
options = {
formats: formats,
widths: widths,
urlPath: "/images/ctfl",
outputDir: "dist/images/ctfl",
filenameFormat: function (id, src, width, format, options) {
return `${imgId}-${imgWidth}x${imgHeight}-${width}w-${fit}-${focus}.${format}`;
},
};
let stats;
if (process.env.ELEVENTY_SERVERLESS) {
stats = eleventyImage.statsByDimensionsSync(
imgUrl,
imgWidth,
imgHeight,
options
);
} else {
stats = await eleventyImage(imgUrl, options);
}
return eleventyImage.generateHTML(stats, {
alt,
loading: "lazy",
decoding: "async",
sizes: sizes,
class: classes,
});
}
module.exports = ctflPictureShortcode;