-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (44 loc) · 1.26 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
49
var globby = require('globby');
var gm = require('gm').subClass({imageMagick: true});
var mkdirp = require('mkdirp');
var async = require('async');
var path = require('path');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
function Resizer(src, target, size) {
EventEmitter.call(this);
if ( typeof(size) === 'string' ) {
var sizes = size.split('x');
size = {
width:parseInt(sizes[0]),
height:parseInt(sizes[1])
};
}
this._src = src;
this._target = target;
this._size = size;
}
util.inherits(Resizer, EventEmitter);
Resizer.prototype.resize = function(file, done) {
var self = this;
var fileSrc = path.join(this._src, file);
var fileDest = path.join(this._target, file);
var folder = path.dirname(fileDest);
mkdirp(folder, function() {
self.emit('image', file, self._size.width + 'x' + self._size.height);
var options = '!';
gm(fileSrc).resize(self._size.width, self._size.height, options)
.write(fileDest, done);
});
}
module.exports = function(src, glob, target, size, done) {
var resizer = new Resizer(src, target, size);
globby(glob, {
cwd: src
}, function(err, files) {
async.forEach(files, function(file, nextFile) {
resizer.resize(file, nextFile);
}, done);
});
return resizer;
};