forked from Alipakkr/Voyawander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateThumbnails.js
69 lines (65 loc) · 1.72 KB
/
generateThumbnails.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
import { faker } from "@faker-js/faker";
import bcrypt from "bcrypt";
// https://chancejs.com/usage/node.html
import {
recipeCategories,
ingredients,
recipes,
areas,
} from "./raw-data/meals.js";
import sharp from "sharp";
import dotenv from "dotenv";
import { readdir } from "node:fs/promises";
import { existsSync } from "node:fs";
import path from "node:path";
dotenv.config();
// just change base path; then run node generateThumbnails.js
const basePath = "public/images-raw/meals";
const toPath = "public/images/meals"
const optimizeSelf = true;
const generateThumb = false;
const files = await readdir(basePath);
for (const file of files) {
let filePath = basePath + "/" + file;
let perhapsThumbPath =
basePath + "/" + path.parse(file).name + "-thumb" + path.parse(file).ext;
// optimize self
if (optimizeSelf && !path.parse(file).name.endsWith('thumb')) {
sharp(filePath)
.resize(400)
.jpeg(70)
.toFile(
toPath +
"/" +
path.parse(file).name +
path.parse(file).ext,
(err, resizeImage) => {
if (err) {
console.log(err);
} else {
console.log(resizeImage + " " + filePath);
}
}
);
}
// generate thumb
if (generateThumb && !path.parse(file).name.endsWith('thumb') && !existsSync(perhapsThumbPath)) {
sharp(filePath)
.resize(100, 100)
.png(70)
.toFile(
toPath +
"/" +
path.parse(file).name +
"-thumb" +
path.parse(file).ext,
(err, resizeImage) => {
if (err) {
console.log(err);
} else {
console.log(resizeImage + " " + filePath);
}
}
);
}
}