Created
January 16, 2013 17:57
-
-
Save ndrut/4549230 to your computer and use it in GitHub Desktop.
Render an image in base64 using phantom js. Convert the base64 into a raw binary buffer, pass the binary buffer to GraphicsMagic which can write to a writable stream, collect the chunks of data in separate buffers, storing them in an array and then concatenate them all together.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PhantomJS Render to base64 function | |
page.renderBase64('png', function(err, img) { | |
var image = {}; | |
image.thumb = []; | |
final.full = new Buffer(img, 'base64'); // Convert the base64 encoded value into binary. | |
var thumbStream = new stream.Stream(); // Create a new writable stream | |
thumbStream.writable = true; | |
thumbStream.write = function (chunk) { | |
image.thumb.push(chunk); // Take the data written and store it into an array | |
}; | |
thumbStream.end = function () { | |
this.emit('close'); // Cleanup the stream. | |
}; | |
gm(final.full, 'img.jpg') // Enter GraphicsMagic | |
.resize('200', '200') // ... Resize the image to 200x200 pixels | |
.stream(function (err, stdout, stderr) { | |
if(err) console.log(err); | |
if (stderr) console.log(stderr); | |
stdout.pipe(thumbStream); // Read stdout and write it to our writable stream | |
thumbStream.on('close', function () { | |
final.thumb = Buffer.concat(image.thumb); // When the stream is done, concatenate the images | |
callback(final); | |
ph.exit(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment