/
imagick.ts
237 lines (198 loc) Β· 5.92 KB
/
imagick.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { getPathAndExtension, merge } from "../core/utils.ts";
import binaryLoader from "../core/loaders/binary.ts";
import { Exception } from "../core/errors.ts";
import { ImageMagick, initialize } from "../deps/imagick.ts";
import Cache from "../core/cache.ts";
import type { Page, Site } from "../core.ts";
import type { IMagickImage, MagickFormat } from "../deps/imagick.ts";
await initialize();
export interface Options {
/** The list extensions this plugin applies to */
extensions: string[];
/** The key name for the transformations definitions */
name: string;
/** The cache folder */
cache: string | boolean;
/** Custom transform functions */
functions: Record<string, TransformationFunction>;
}
export type TransformationFunction = (
image: IMagickImage,
// deno-lint-ignore no-explicit-any
...args: any[]
) => void;
// Default options
export const defaults: Options = {
extensions: [".jpg", ".jpeg", ".png"],
name: "imagick",
cache: true,
functions: {
resize(image: IMagickImage, width: number, height = width): void {
image.resize(width, height);
},
crop(image: IMagickImage, width: number, height = width): void {
image.crop(width, height);
},
blur(image: IMagickImage, radius: number, sigma: number): void {
image.blur(radius, sigma);
},
sharpen(image: IMagickImage, radius: number, sigma: number): void {
image.sharpen(radius, sigma);
},
rotate(image: IMagickImage, degrees: number): void {
image.rotate(degrees);
},
autoOrient(image: IMagickImage): void {
image.autoOrient();
},
},
};
export interface Transformation {
suffix?: string;
format?: MagickFormat | MagickFormat[];
matches?: RegExp | string;
// deno-lint-ignore no-explicit-any
[key: string]: any;
}
interface SingleTransformation extends Transformation {
format?: MagickFormat;
}
/** A plugin to transform images in Lume */
export default function (userOptions?: Partial<Options>) {
const options = merge(defaults, userOptions);
return (site: Site) => {
site.loadAssets(options.extensions, binaryLoader);
site.process(options.extensions, imagick);
// Configure the cache folder
const cacheFolder = options.cache === true ? "_cache" : options.cache;
const cache = cacheFolder
? new Cache({ folder: site.src(cacheFolder) })
: undefined;
if (cacheFolder) {
site.ignore(cacheFolder);
site.options.watcher.ignore.push(cacheFolder);
}
async function imagick(page: Page, pages: Page[]) {
const imagick = page.data[options.name] as
| Transformation
| Transformation[]
| undefined;
if (!imagick) {
return;
}
const content = page.content as Uint8Array;
const transformations = getTransformations(imagick);
let transformed = false;
let index = 0;
for (const transformation of transformations) {
if (transformation.matches) {
const regex = new RegExp(transformation.matches);
if (!regex.test(page.data.url as string)) {
continue;
}
}
const output = page.duplicate(index++, {
...page.data,
[options.name]: undefined,
});
rename(output, transformation);
if (cache) {
const result = await cache.get(content, transformation);
if (result) {
output.content = result;
} else {
transform(content, output, transformation, options);
transformed = true;
await cache.set(content, transformation, output.content!);
}
} else {
transform(content, output, transformation, options);
transformed = true;
}
if (output !== page) {
pages.push(output);
}
}
if (transformed) {
site.logger.log("π¨", `${page.src.path}${page.src.ext}`);
}
// Remove the original page
return false;
}
};
}
function transform(
content: Uint8Array,
page: Page,
transformation: Transformation,
options: Options,
): void {
let format: MagickFormat | undefined = undefined;
ImageMagick.read(content, (image: IMagickImage) => {
for (const [name, args] of Object.entries(transformation)) {
switch (name) {
case "suffix":
case "matches":
break;
case "format":
format = args;
break;
default:
if (!options.functions[name]) {
throw new Exception(`Unknown transformation: ${name}`);
}
if (Array.isArray(args)) {
options.functions[name](image, ...args);
} else {
options.functions[name](image, args);
}
}
}
if (format) {
image.write(
format,
(content: Uint8Array) => page.content = new Uint8Array(content),
);
} else {
image.write((content: Uint8Array) =>
page.content = new Uint8Array(content)
);
}
});
}
function rename(page: Page, transformation: Transformation): void {
const { format, suffix } = transformation;
const url = page.data.url;
if (!url) {
return;
}
let [path, ext] = getPathAndExtension(url);
if (format) {
ext = `.${format}`;
}
if (suffix) {
path += suffix;
}
page.data.url = path + ext;
}
function getTransformations(
input: Transformation | Transformation[],
): SingleTransformation[] {
if (Array.isArray(input)) {
const singles: SingleTransformation[] = [];
for (const transformation of input) {
if (Array.isArray(transformation.format)) {
transformation.format.forEach((format) => {
singles.push({ ...transformation, format });
});
} else {
singles.push(transformation as SingleTransformation);
}
}
return singles;
}
if (Array.isArray(input.format)) {
return input.format.map((format) => ({ ...input, format }));
}
return [input as SingleTransformation];
}