-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresizeImages.js
More file actions
66 lines (59 loc) · 2.07 KB
/
resizeImages.js
File metadata and controls
66 lines (59 loc) · 2.07 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
const Jimp = require("jimp");
const fs = require("fs");
const path = require("path");
const covers = path.join(__dirname, "src/static/images/");
const designs = path.join(__dirname, "src/static/images/designs");
const posts = path.join(__dirname, "src/static/images/posts");
const moveToCovers = path.join(__dirname, "src/static/images/resizedImages");
async function resize(imageType, imageUrl, fileName) {
let width;
let height;
switch (imageType) {
case "covers":
(width = 100), (height = Jimp.AUTO);
break;
case "designs":
(width = 300), (height = Jimp.AUTO);
break;
case "posts":
(width = 300), (height = Jimp.AUTO);
break;
default:
break;
}
const image = await Jimp.read(imageUrl);
const outputPath = path.join(moveToCovers, "resized-" + fileName);
await image.resize(width, height);
await image.writeAsync(outputPath);
}
resize("covers", path.join(__dirname, "src/static/images/Final_logo.png"), "Final_logo.png");
// Make an async function that gets executed immediately
async function resizeDirectory(folderPath, imageType) {
// Our starting point
try {
// Get the files as an array
const files = await fs.promises.readdir(folderPath);
// Loop them all with the new for...of
for (const file of files) {
// Get the full paths
const fromPath = path.join(folderPath, file);
const toPath = path.join(moveToCovers, file);
// Stat the file to see if we have a file or dir
const stat = await fs.promises.stat(fromPath);
if (stat.isFile()) {
await resize(imageType, fromPath, file);
console.log("'%s' done resizing.", fromPath);
} else if (stat.isDirectory()) {
console.log("'%s' is a directory.", fromPath);
}
// Now move async
// await fs.promises.rename(fromPath, toPath);
// Log because we're crazy
// console.log("Moved '%s'->'%s'", fromPath, toPath);
} // End for...of
} catch (e) {
// Catch anything bad that happens
console.error("We've thrown! Whoops!", e);
}
}
// resizeDirectory(covers, "covers");