似乎我在使用 proc_open() php 函数时使用管道传输到进程的流时遇到问题。
我开始的过程只是转换 ImageMagick 实用程序,将 3 个图像叠加在一起。当仅使用 1 个输入流 (STDIN) 并将一个变量转储到该流中时,转换程序可以正常工作并返回其输出,该输出可以存储在一个变量中,如下所示:
$cmd = BIN_PATH.DIRECTORY_SEPARATOR.'convert ';
$cmd .= ' -size SOMESIZE ';
$cmd .= ' -background black ';
$cmd .= ' -fill white ';
$cmd .= ' -stroke none ';
$cmd .= ' -gravity center ';
$cmd .= ' -trim ';
$cmd .= ' -interline-spacing SOMELINEHEIGHT ';
$cmd .= ' -font SOMEFONT ';
$cmd .= ' label:"SOMETEXT" ';
$cmd .= ' miff:- ';
$ctext_opacity = shell_exec($cmd);
首先,我运行转换并将输出存储在 $ctext_opacity 变量中。然后通过 proc_open() 调用下一个命令,并且 $ctext_opacity 变量通过 STDIN 管道传输并用作输入图像:
$cmd = BIN_PATH.DIRECTORY_SEPARATOR.'convert ';
$cmd .= '-size SOMESIZE ';
$cmd .= ' xc:\'rgb(230, 225, 50)\' ';
$cmd .= ' -gravity center ';
$cmd .= ' - '; // ImageMagick uses dash(-) for STDIN
$cmd .= ' -alpha Off ';
$cmd .= ' -compose CopyOpacity ';
$cmd .= ' -composite ';
$cmd .= ' -trim ';
$cmd .= ' miff:- ';
$chighlight = '';
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w")
);
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $ctext_opacity);
fclose($pipes[0]);
while (!feof($pipes[1])) {
$chighlight .= fgets($pipes[1]); // HERE WE FEED THE OUTPUT OF "CONVERT" TO $chighlight
}
//echo $chighlight; die();
fclose($pipes[1]);
$return_value = proc_close($process);
}
上述命令被调用 3 次,生成了 3 个单独的图像并存储在 3 个变量中。下一个命令应该接受这 3 个变量作为输入图像(ImageMagic 语法指定替代 io 流,如 fd:N,其中 N 是我通过 proc_open() 生成的流的编号)。但是,我似乎正在错误地写入输入流或从 STDOUT 读取,这很可能导致进程未刷新输出,导致进程挂起而不终止。
$cmd = BIN_PATH.DIRECTORY_SEPARATOR.'convert ';
$cmd .= ' -size SOMESIZE ';
$cmd .= ' xc:transparent ';
$cmd .= ' -gravity center ';
$cmd .= ' - -geometry -2-2 -composite ';
$cmd .= ' fd:3 -geometry +2+2 -composite ';
$cmd .= ' fd:4 -composite ';
$cmd .= 'png:- ';
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "a"),
3 => array("pipe", "r"),
4 => array("pipe", "r")
);
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
$read = null;
$rd = array($pipes[1]);
$write = array($pipes[0], $pipes[3], $pipes[4]);
$wt = array($pipes[0], $pipes[3], $pipes[4]);
$im_args = array($cshade, $chighlight, $ctext);
$except = null;
$readTimeout = 1;
$ctext_deboss = '';
$numchanged = stream_select($read, $write, $except, $readTimeout);
foreach($write as $w) {
$key = array_search($w, $wt);
fwrite($wt[$key], $im_args[$key]);
fclose($wt[$key]);
$read = array($pipes[1]);
$rd = array($pipes[1]);
$write = null;
$except = null;
$readTimeout = 1;
$ctext_deboss = '';
$numchanged = stream_select($read, $write, $except, $readTimeout);
foreach($read as $r) {
while (!feof($r)) {
$ctext_deboss .= fgets($pipes[1]);
}
}
fclose($pipes[1]);
$return_value = proc_close($process);
echo $ctext_deboss; die();
}
}
我似乎无法传输 3 和 4 管道的内容,因为 convert 会抛出空/不正确数据的错误