node.js - Imagemagick/Shell/子进程 NodeJS
问题描述
我似乎无法让 Node 理解我的 Imagemagick 转换命令。它在终端中运行良好,我尝试了很多不同的排列来转义字符,但它仍然读取我的 args 数组中的每个单独的字符串值,以尝试转换图像:
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:
这在 shell 中运行正常。但是,此代码完全失败
// 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
});
解决方案
明白了....可能效率低下,但:耸肩
// 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();
});
推荐阅读
- sql - 外部联接不适用于现有查询
- python - 尝试验证任何 ARM 模板但得到:'错误将值 <...MyTemplate...> 转换为类型'....Templates.Schema.Template'
- mysql - 列组作为 Mysql 中的唯一键,但使用时间戳作为一天
- python - 为什么 pytest 覆盖跳过自定义异常消息断言?
- c# - NSNetService 使用 C# 分配 IP 地址
- c++ - 永远不会调用日志记录挂钩的挂钩程序
- python - master 和 worker 之间的通信:使用 http 请求或 scp 文件并监控文件创建是否更好?
- html - How to Reduce Clickable Area of Links in Css
- r - sum 函数的奇怪行为
- javascript - 当我在输入值中键入时,我希望该输入值在我键入时作为文本内容出现在 span 标记内