-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFrame.ts
More file actions
114 lines (104 loc) · 3.32 KB
/
ImageFrame.ts
File metadata and controls
114 lines (104 loc) · 3.32 KB
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
import { FileGroup, FileInfo } from "../types/index.ts";
import Plugin from "../models/Plugin.ts";
import Post from "../models/Post.ts";
import sharp from "sharp";
interface ImageFrameSettings {
inner_width: string | number; // set to 0 or "" to ignore
inner_color: string; // css color
outer_width: string | number; // set to 0 or "" to ignore
outer_color: string; // css color
}
// https://sharp.pixelplumbing.com/api-resize#extend
/**
* Plugin ImageFrame.
*
* Add single or double border around images from Post
*
*/
export default class ImageFrame extends Plugin {
static defaults: ImageFrameSettings = {
inner_width: "1%",
inner_color: "black",
outer_width: "9%",
outer_color: "white",
};
settings: ImageFrameSettings;
constructor(settings?: object) {
super();
this.settings = {
...ImageFrame.defaults,
...(settings ?? {}),
};
}
/**
* Process the post
*/
async process(post: Post): Promise<void> {
post.platform.user.log.trace(this.id, post.id, "process");
for (const file of post.getFiles(FileGroup.IMAGE)) {
await this.addImageFrame(post, file);
}
}
private async addImageFrame(post: Post, file: FileInfo) {
if (file.width && file.height) {
const size = Math.min(file.width, file.height);
const newFileName = file.basename + "-framed." + file.extension;
const src = file.name;
const dst = post.platform.assetsFolder + "/" + newFileName;
const fileIn = post.getFilePath(src);
const fileOut = post.getFilePath(dst);
const bufferIn = await post.platform.user.files.readBuffer(fileIn);
const source = sharp(bufferIn);
let innerBuffer = await source.toBuffer();
if (this.settings.inner_width) {
let inner_width = 0;
if (typeof this.settings.inner_width === "string") {
if (this.settings.inner_width.endsWith("%")) {
inner_width = Math.round(
(size * parseInt(this.settings.inner_width)) / 100,
);
} else {
inner_width = parseInt(this.settings.inner_width);
}
} else {
inner_width = this.settings.inner_width;
}
innerBuffer = await source
.extend({
top: inner_width,
bottom: inner_width,
left: inner_width,
right: inner_width,
background: this.settings.inner_color,
})
.toBuffer();
}
let outerBuffer = innerBuffer;
if (this.settings.outer_width) {
let outer_width = 0;
if (typeof this.settings.outer_width === "string") {
if (this.settings.outer_width.endsWith("%")) {
outer_width = Math.round(
(size * parseInt(this.settings.outer_width)) / 100,
);
} else {
outer_width = parseInt(this.settings.outer_width);
}
} else {
outer_width = this.settings.outer_width;
}
outerBuffer = await sharp(innerBuffer)
.extend({
top: outer_width,
bottom: outer_width,
left: outer_width,
right: outer_width,
background: this.settings.outer_color,
})
.toBuffer();
}
await post.platform.user.files.write(fileOut, outerBuffer);
await post.replaceFile(src, dst);
}
}
}