When trying to read data in Node.js from an ImageMagick child process, it comes out corrupted.
A simple test case would be the following:
var fs = require('fs');
var exec = require('child_process').exec;
var cmd = 'convert ./test.jpg -';
exec(cmd, {encoding: 'binary', maxBuffer: 5000*1024}, function(error, stdout) {
fs.writeFileSync('test2.jpg', stdout);
});
I would expect that to be the equivalent of the command line convert ./test.jpg - > test2.jpg
that does write the binary file correctly.
Originally there was a problem with the maxBuffer option being too small and resulting in a truncated file. After increasing that, the file now appears slightly larger than expected and still corrupted. The data from stdout is required to send over HTTP.
What would be the correct way to read this data from the ImageMagick stdout?
Binary is simply a set or a collection of 1 and 0 . Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations. For example, the following are five different binaries: 10, 01, 001, 1110, 00101011.
Try this . var fs = require("fs"); fs. readFile('image. jpg', function(err, data) { if (err) throw err; // Encode to base64 var encodedImage = new Buffer(data, 'binary').
To execute an external program from within Node. js, we can use the child_process module's exec method. const { exec } = require('child_process'); exec(command, (error, stdout, stderr) => { console.
There were two problems with the initial approach.
The maxBuffer needs to be high enough to handle the whole response from the child process.
Binary encoding needs to be properly set everywhere.
A full working example would be the following:
var fs = require('fs');
var exec = require('child_process').exec;
var cmd = 'convert ./test.jpg -';
exec(cmd, {encoding: 'binary', maxBuffer: 5000*1024}, function(error, stdout) {
fs.writeFileSync('test2.jpg', stdout, 'binary');
});
Another example, sending the data in an HTTP response using the Express web framework, would like this:
var express = require('express');
var app = express.createServer();
app.get('/myfile', function(req, res) {
var cmd = 'convert ./test.jpg -';
exec(cmd, {encoding: 'binary', maxBuffer: 5000*1024}, function(error, stdout) {
res.send(new Buffer(stdout, 'binary'));
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With