Reputation: 285
I can't seem to get Node to understand my Imagemagick convert command. It works fine in the terminal, I've tried a ton of different permutations for escaping characters but it still reads every seperate string value in my args array as an attempt to convert an image:
convert \
input.jpg \
-write mpr:XY \
+delete \
-respect-parentheses \
\( mpr:XY -resize x640 -sampling-factor 4:2:0 -strip -interlace Plane +write output-640.jpg \) \
\( mpr:XY -resize x320 -sampling-factor 4:2:0 -strip -interlace Plane +write output-320.jpg \) \
\( mpr:XY -resize x155 -sampling-factor 4:2:0 -strip -interlace Plane +write output-155.jpg \) \
\( mpr:XY -resize x45 -sampling-factor 4:2:0 -strip -interlace Plane +write output-45.jpg \) \
null:
This ran in a shell works fine. This code however, fails totally
// const spawn = require('child-process-promise').spawn;
var spawn = require('child_process').spawn;
function argsForSize(number) {
return [
'\\(',
'mpr:XY',
'-resize',
'x' + number,
'-sampling-factor',
'4:2:0',
'-strip',
'-interlace',
'Plane',
'+write',
'output-' + number +'.jpg',
'\\)\\'
]
}
let inputPath = '/Users/xconanm/Desktop/imagemagick/test-app/input.jpg';
var args = [
inputPath, '\\',
'-write', 'mpr:XY', '\\', // resize width to 640
'+delete', '\\',
'-respect-parentheses', '\\'
];
let extraArgs = argsForSize(640).concat(argsForSize(320)).concat(argsForSize(155)).concat(argsForSize(55));
extraArgs.push('null:');
args = args.concat(extraArgs)
let child = spawn('convert', args)
console.log(args);
child.stdout.on('data', function(data) {
console.log('stdout: ' + data);
//Here is where the output goes
});
child.stderr.on('data', function(data) {
console.log('stderr: ' + data);
//Here is where the error output goes
});
child.on('close', function(code) {
console.log('closing code: ' + code);
//Here you can get the exit code of the script
});
Upvotes: 3
Views: 962
Reputation: 285
Got it.... Probably inefficient but :shrug
// const spawn = require('child-process-promise').spawn;
var spawn = require('child_process').spawn;
function argsForSize(number) {
return [
'-write',
'mpr:XY',
'+delete',
'-respect-parentheses',
'mpr:XY',
'-resize',
'x' + number,
'-sampling-factor',
'4:2:0',
'-strip',
'-interlace',
'Plane',
'-write',
'output-' + number +'.jpg'
]
}
let inputPath = '/Users/xconanm/Desktop/imagemagick/test-app/input.jpg';
var args = [
inputPath
];
let extraArgs = argsForSize(640).concat(argsForSize(320)).concat(argsForSize(155)).concat(argsForSize(55));
extraArgs.push('null:');
args = args.concat(extraArgs)
let child = spawn('convert', args)
console.log(args);
child.stdout.on('data', function(data) {
console.log('stdout: ' + data);
//Here is where the output goes
});
child.stderr.on('data', function(data) {
console.log('stderr: ' + data);
//Here is where the error output goes
});
child.on('close', function(code) {
console.log('closing code: ' + code);
//Here you can get the exit code of the script
process.exit();
});
Upvotes: 3