-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize.js
More file actions
57 lines (44 loc) · 1.78 KB
/
resize.js
File metadata and controls
57 lines (44 loc) · 1.78 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
import fs from "fs";
import path from "path";
import sharp from "sharp";
async function main() {
const srcDir = "./input";
const destDir = "./output";
const inputFileNames = await fs.promises.readdir(srcDir);
for (let i = 0; i < inputFileNames.length; i++) {
const fileFullName = inputFileNames[i];
const extension = path.extname(fileFullName);
const fileName = fileFullName.replace(extension, "");
const src = path.join(srcDir, fileFullName);
const destOpenGraph = path.join(destDir, `${fileName}.open-graph.webp`);
const destTwitter = path.join(destDir, `${fileName}.twitter.webp`);
console.debug({ src, destOpenGraph, destTwitter });
// Open Graph
await transform(src, destOpenGraph, 1200, 628);
// Twitter
await transform(src, destTwitter, 800, 418);
}
}
async function transform(src, dest, width, height) {
const metadata = await sharp(src).metadata();
// Calculate the source and the target aspect ratio
const srcAspectRatio = metadata.width / metadata.height;
const destAspectRatio = width / height;
// Resize the image so that it covers the target dimensions, apply blur and
// store the result in memory
const backgroundBuffer = await sharp(src)
.resize({ width, height, fit: "cover" })
.blur(10)
.toBuffer();
// Resize the image so that it's contained within the target dimensions and
// store the result in memory
const foregroundBuffer = await sharp(src)
.resize(srcAspectRatio > destAspectRatio ? { width } : { height })
.toBuffer();
// Combine the background and the foreground and store the result in a file
await sharp(backgroundBuffer)
.composite([{ input: foregroundBuffer, gravity: "center" }])
.webp({ quality: 80 })
.toFile(dest);
}
main().catch((err) => console.error(err));