-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (40 loc) · 1.3 KB
/
index.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
var inputImagePath = process.argv[2]
var Imagina = require('imagina')
var path = require('path')
var dir = path.dirname(inputImagePath)
var extension = path.extname(inputImagePath).substring(1)
var filename = path.basename(inputImagePath, '.' + extension)
var resizedImagePath = path.join(dir, filename + '.resized.' + extension)
var im = new Imagina()
resize(function onResizeDone(err) {
if (err) {
throw err
}
console.log('resize done')
if (extension !== 'png') {
convert(resizedImagePath, function onConvertDone(err) {
if (err) {
throw err
}
console.log('Conversion done')
})
} else {
console.log('no conversion necessary');
}
})
function convert(inputPath, cb) {
var dir = path.dirname(inputPath)
var extension = path.extname(inputPath).substring(1)
var filename = path.basename(inputPath, '.' + extension)
var outputPath = path.join(dir, filename + '.png')
console.log('converting', inputPath, outputPath)
im.convert(inputPath, outputPath, cb)
}
function resize(cb) {
var params = '-resize 180x180^ -gravity center -extent 180x180'.split(' ')
console.log('resizing', inputImagePath, resizedImagePath)
im.resize(inputImagePath, resizedImagePath, '180x180', params, cb)
}
function getFilename(filepath) {
return filepath.split(/(\\|\/)/g).pop()
}