-
Notifications
You must be signed in to change notification settings - Fork 1
/
resize.js
70 lines (55 loc) · 2.83 KB
/
resize.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
70
/* eslint-disable @typescript-eslint/no-var-requires */
const sharp = require("sharp")
const glob = require("glob")
// const matches = glob.sync(`src/images/asia/vietnam/**/*.{png,jpg,jpeg}`)
// const matches = glob.sync(`src/images/asia/philippines/el-nido/*.{png,jpg,jpeg}`)
const matches1 = glob.sync(`Photos/h/*.{png,jpg,jpeg,JPG}`)
const matches2 = glob.sync(`Photos/w/*.{png,jpg,jpeg,JPG}`)
const matches3 = glob.sync(`Photos/p/*.{png,jpg,jpeg,JPG}`)
// const MAX_WIDTH = 1200 // portrait
// const MAX_WIDTH = 1800
const QUALITY = 60
Promise.all(
matches3.map(async (match) => {
console.log(match)
const stream = sharp(match)
const info = await stream.metadata()
// const width = Math.min(200, info.width)
const width = Math.min(1000, info.width)
const optimizedName = match.replace(/(\..+)$/, (match, ext) => `-optimized${ext.toLowerCase()}`)
const method = info.format === "png" ? "png" : "jpeg"
// await stream.resize(width)[method]({ quality: 50 }).toFile(optimizedName)
await stream.resize(width)[method]({ quality: QUALITY }).withMetadata().toFile(optimizedName)
// return fs.rename(optimizedName, match)
})
)
Promise.all(
matches1.map(async (match) => {
console.log(match)
const stream = sharp(match)
const info = await stream.metadata()
const width = Math.min(1200, info.width)
const optimizedName1 = match.replace(/(\..+)$/, (match, ext) => `-optimized-${width}-60${ext.toLowerCase()}`)
const optimizedName2 = match.replace(/(\..+)$/, (match, ext) => `-optimized-${width}-80${ext.toLowerCase()}`)
const method = info.format === "png" ? "png" : "jpeg"
await stream.resize(width)[method]({ quality: 60, mozjpeg: true }).withMetadata().toFile(optimizedName1)
await stream.resize(width)[method]({ quality: 80, mozjpeg: true }).withMetadata().toFile(optimizedName2)
// return fs.rename(optimizedName, match)
})
)
Promise.all(
matches2.map(async (match) => {
console.log(match)
const stream = sharp(match)
const info = await stream.metadata()
const width = Math.min(1800, info.width)
const optimizedName1 = match.replace(/(\..+)$/, (match, ext) => `-optimized-${width}-60${ext.toLowerCase()}`)
const optimizedName2 = match.replace(/(\..+)$/, (match, ext) => `-optimized-${width}-70${ext.toLowerCase()}`)
const optimizedName3 = match.replace(/(\..+)$/, (match, ext) => `-optimized-${width}-80${ext.toLowerCase()}`)
const method = info.format === "png" ? "png" : "jpeg"
await stream.resize(width)[method]({ quality: 60, mozjpeg: true }).withMetadata().toFile(optimizedName1)
await stream.resize(width)[method]({ quality: 70, mozjpeg: true }).withMetadata().toFile(optimizedName2)
await stream.resize(width)[method]({ quality: 80, mozjpeg: true }).withMetadata().toFile(optimizedName3)
// return fs.rename(optimizedName, match)
})
)