-
Notifications
You must be signed in to change notification settings - Fork 0
/
genThumbs.js
55 lines (50 loc) · 1.43 KB
/
genThumbs.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
var gm = require('gm').subClass({ imageMagick: true });
var fs = require('fs');
var path = require('path');
var targetH = 150;
var root = process.argv[2];
if(!root) throw "no path specified";
root = path.resolve(root);
fs.readdir(root, function(err, files) {
if(fs.existsSync(root + '/thumbnails'))
rmdir(root + '/thumbnails');
fs.mkdirSync(root + '/thumbnails');
files.forEach(function(name) {
if(!isImage(name)) return;
var file = root + '/' + name;
var thumb = root + '/thumbnails/' + removeExt(name) + '_thumb.png';
var _size = 0;
gm(file)
.size(function (err, size) {
if (err) throw err;
_size = size;
})
.resize(Math.round(targetH/_size.height*_size.width), targetH)
.noProfile()
.write(thumb, function (err) {
if (!err) gm(file);
else throw err;
});
});
});
function isImage(path) {
var splitz = path.split('.');
var ext = splitz[splitz.length-1].toLowerCase();
return (ext == 'jpg' || ext == 'png');
}
function removeExt(str) {
return str.substring(0, str.lastIndexOf('.'));
}
var rmdir = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};