-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (27 loc) · 941 Bytes
/
index.js
File metadata and controls
36 lines (27 loc) · 941 Bytes
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
var fs = require('fs'),
SmartCrop = require('smartcrop'),
Canvas = require('canvas'),
_ = require('underscore');
exports.defaults = {
quality: 90,
width: 500,
height: 500,
canvasFactory: function(w, h){ return new Canvas(w, h); },
input: null,
output: null,
}
exports.crop = function(opts){
var opts = _.extend({}, this.defaults, opts);
var img = new Canvas.Image();
img.src = fs.readFileSync(opts.input);
SmartCrop.crop(img, opts, function(result){
var canvas = new Canvas(opts.width, opts.height),
ctx = canvas.getContext('2d'),
crop = result.topCrop,
f = fs.createWriteStream(opts.output);
ctx.patternQuality = 'best';
ctx.filter = 'best';
ctx.drawImage(img, crop.x, crop.y, crop.width, crop.height, 0, 0, canvas.width, canvas.height);
canvas.syncJPEGStream({quality: opts.quality}).pipe(f);
});
}