-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
imagick.ts
182 lines (152 loc) Β· 4.8 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
import { merge } from "../core/utils.ts";
import binaryLoader from "../core/loaders/binary.ts";
import { Exception } from "../core/errors.ts";
import { ImageMagick, initializeImageMagick } 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 initializeImageMagick();
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;
// deno-lint-ignore no-explicit-any
[key: string]: any;
}
export type Transformations = Transformation[];
/** 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) {
const imagick = page.data[options.name] as
| Transformation
| Transformations
| undefined;
if (!imagick) {
return;
}
const content = page.content as Uint8Array;
const transformations = Array.isArray(imagick) ? imagick : [imagick];
const last = transformations[transformations.length - 1];
let transformed = false;
let index = 0;
for (const transformation of transformations) {
const output = transformation === last
? page
: page.duplicate(index++, { [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) {
site.pages.push(output);
}
}
if (transformed) {
site.logger.log("π¨", `${page.src.path}${page.src.ext}`);
}
}
};
}
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":
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);
}
}
}
image.write(
(content: Uint8Array) => page.content = new Uint8Array(content),
format,
);
});
}
function rename(page: Page, transformation: Transformation): void {
if (transformation.format) {
page.updateDest({ ext: "." + transformation.format });
}
if (transformation.suffix) {
page.updateDest({ path: page.dest.path + transformation.suffix });
}
}