1

GM は、次のような標準入力からのバイナリ データの受け渡しをサポートしています。

gm convert gif:- jpg:-

gmコンポジットを使用して、ある画像を別の画像の上に重ねて透かしを作成しようとしています:

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

ただし、私の PHP コードには、$orig_str と $watermark_str という 2 つの文字列があり、どちらもそれぞれ orig.jpg と watermark.jpg のバイナリ データです。これら 2 つの文字列を stdin として渡して上記を実行しようとしていますが、その方法がわかりません。

$orig_str を変更しても問題ありません。

アーキテクチャ上の理由から、PHP の GM プラグインを使用せずに 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ね!

実行するコマンドを渡し、次に、プロセスのstdin、stdout、およびstderrを表すために開くストリームの説明を含む配列を渡します。

ストリームは事実上ファイルハンドルであるため、ファイルに書き込んでいるかのように簡単に書き込むことができます。

たとえば、私自身のコードベースの印刷ビットから:

// 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 に答える