1

GM 支持从标准输入传入二进制数据,如下所示:

gm convert gif:- jpg:-

我正在尝试使用 gm 合成在另一个图像之上使用一个图像创建水印:

gm composite -geometry +0+0 orig.jpg watermark.jpg new.jpg

但是,在我的 PHP 代码中,我有两个字符串,$orig_str 和 $watermark_str,它们分别是 orig.jpg 和 watermark.jpg 的二进制数据。我试图通过将这两个字符串作为标准输入传递来运行上述内容,但无法找到这样做的方法。

修改 $orig_str 没问题。

出于架构原因,我正在执行 GM 而不使用 PHP 的 GM 插件。相反,我正在做这样的事情来运行 gm:

$img = "binary_data_here";
$cmd = ' gm convert gif:- jpg:-';
$stdout = execute_stdin($cmd, $img);

function execute_stdin($cmd, $stdin /* $arg1, $arg2 */) {...}

有谁知道如何为标准输入中的多个输入执行此操作?

4

1 回答 1

0

听起来像是一份工作proc_open

您将向它传递要运行的命令,然后是包含要打开的流的描述的数组,以表示该进程的标准输入、标准输出和标准错误。

流实际上是文件句柄,因此您可以像写入文件一样简单地写入它们。

例如,从我自己的代码库中的打印位:

// In this case, $data is a PDF document that we'll feed to
// the stdin of /usr/bin/lp
    $data = '';
    $handles = array(
        0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
        1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
        2 => array("pipe", "a")   // stderr is a file to write to
    );
// Setting of $server, $printer_name, $options_flag omitted...
    $process_name = 'LC_ALL=en_US.UTF-8 /usr/bin/lp -h %s -d %s %s';
    $command = sprintf($process_name, $server, $printer_name, (string)$options_flag);
    $pipes = array();
    $process = proc_open($command, $handles, $pipes);
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// As we've been given data to write directly, let's kinda like do that.
    fwrite($pipes[0], $data);
    fclose($pipes[0]);
// 1 => readable handle connected to child stdout
    $stdout = fgets($pipes[1]);
    fclose($pipes[1]);
// 2 => readable handle connected to child stderr
    $stderr = fgets($pipes[2]);
    fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
    $return_value = proc_close($process);
于 2012-02-08T01:16:41.010 回答