test.js
1009 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
37
38
39
40
41
var
graphicsMagick = require('./build/Release/GraphicsMagick'),
fs = require('fs');
var inputFile = "test.jpg";
var outputFile = "output.jpg";
console.log("Opening "+inputFile+"...")
fs.readFile(inputFile, function (err, data) {
if (err)
throw err;
console.log("Reading image...");
var inputImage = graphicsMagick.image(data);
console.log("Image has following properties: ");
console.log("Width: "+inputImage.width);
console.log("Height: "+inputImage.height);
console.log("Creating output image...");
var outputImage = inputImage
.crop(Math.floor(inputImage.width/2 - 200/2), Math.floor(inputImage.height/2 - 200/2), 200, 200)
.resize(100, 100);
console.log("Creating write stream for "+outputFile+"...");
var writeStream = fs.createWriteStream(outputFile);
writeStream.on("end", function(){
console.log("Done.");
});
console.log("Writing new image data...");
writeStream.write(outputImage.buffer);
console.log("Closing stream...");
writeStream.end();
});