forked from startupstockphotos/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
73 lines (60 loc) · 1.68 KB
/
process.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
71
72
73
const path = require('path')
const fs = require('fs-extra')
const jimp = require('jimp')
const log = require('log-update')
function write (img, p, name) {
return img.writeAsync(path.join('static', p, name))
}
function watermark (img) {
return jimp.loadFont(jimp.FONT_SANS_32_WHITE)
.then(font => {
return img.print(font, 0, 0, {
text: 'STARTUP STOCK',
alignmentX: jimp.HORIZONTAL_ALIGN_CENTER,
alignmentY: jimp.VERTICAL_ALIGN_MIDDLE
}, 1000)
})
}
function gen (filepath) {
const name = path.basename(filepath, '.jpg')
return Promise.all([
jimp.read(filepath)
.then(img => img.resize(800, jimp.AUTO))
.then(img => img.pixelate(20))
.then(img => img.quality(30))
.then(img => write(img, name, 'placeholder.jpg')),
jimp.read(filepath)
.then(img => img.resize(2000, jimp.AUTO))
.then(img => img.quality(50))
// .then(watermark)
.then(img => write(img, name, 'display.jpg'))
])
}
/**
* Process
*/
const { _: args } = require('minimist')(process.argv.slice(2))
fs.ensureDir('static')
if (args[0] === 'all') {
let count = 0
fs.readdir('raw', (err, files) => {
files = files.map(f => path.join('raw', f))
const length = files.length
log(`processing ${length} files`)
;(function process (file) {
gen(file).then(() => {
log(`processed ${++count} of ${length} files`)
if (files.length) {
process(files.pop())
} else {
log('processing complete')
}
})
})(files.pop())
})
} else if (/\.jpg/.test(args[0])) {
log(`processing ${args[0]}`)
gen(path.resolve(args[0])).then(() => {
log('processing complete')
})
}