|
@ -7,8 +7,7 @@ var Canvas = require('../lib/canvas') |
|
|
, Image = Canvas.Image |
|
|
, Image = Canvas.Image |
|
|
, fs = require('fs'); |
|
|
, fs = require('fs'); |
|
|
|
|
|
|
|
|
var img = new Image |
|
|
var img = new Image; |
|
|
, start = new Date; |
|
|
|
|
|
|
|
|
|
|
|
img.onerror = function(err){ |
|
|
img.onerror = function(err){ |
|
|
throw err; |
|
|
throw err; |
|
@ -19,21 +18,17 @@ img.onload = function(){ |
|
|
, h = img.height / 2 |
|
|
, h = img.height / 2 |
|
|
, canvas = new Canvas(w, h) |
|
|
, canvas = new Canvas(w, h) |
|
|
, ctx = canvas.getContext('2d'); |
|
|
, ctx = canvas.getContext('2d'); |
|
|
|
|
|
|
|
|
ctx.drawImage(img, 0, 0, w, h, 0, 0, w, h); |
|
|
ctx.drawImage(img, 0, 0, w, h, 0, 0, w, h); |
|
|
|
|
|
|
|
|
var out = fs.createWriteStream(__dirname + '/crop.jpg') |
|
|
var out = fs.createWriteStream(__dirname + '/crop.jpg'); |
|
|
, stream = canvas.createJPEGStream({ |
|
|
|
|
|
bufsize : 2048, |
|
|
|
|
|
quality : 80 |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
stream.on('data', function(chunk, len){ |
|
|
var stream = canvas.createJPEGStream({ |
|
|
out.write(chunk); |
|
|
bufsize : 2048, |
|
|
|
|
|
quality : 80 |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
stream.on('end', function(){ |
|
|
stream.pipe(out); |
|
|
console.log('Cropped and saved in %dms', new Date - start); |
|
|
|
|
|
}); |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
img.src = __dirname + '/images/squid.png'; |
|
|
img.src = __dirname + '/images/squid.png'; |
|
|