2012-02-08 1 views
1
GM이 같은 표준 입력 이진 데이터의 전달을 지원

: I는 GM 복합체를 이용하여 다른 화상의 상부에 하나 개의 영상을 이용하여 워터 마크를 만들려고ImageMagick과/GraphicsMagick PHP 여러 이진 데이터

gm convert gif:- jpg:- 

:

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

하지만, 내 PHP 코드에서, 나는 모두가 각각 orig.jpg 및 watermark.jpg의 이진 데이터이며, 두 개의 문자열, $의 orig_str 및 $ watermark_str 있습니다. 위의 두 문자열을 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 */) {...} 

누구나 표준 입력에서 둘 이상의 입력을 수행하는 방법을 알고 있습니까?

답변

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); 
+0

흠 ... 어떻게 파일 GraphicsMagick으로 처리 사용합니까? GM은 파일 핸들러가 아니라 파일 이름을 사용하는 것으로 보입니다. – porkeypop

+0

흠. 질문은 "외부 프로세스를 시작하고 데이터를 표준으로 가져 오는 방법"이라고 생각했습니다. 실제로 "이미지 데이터를 GraphicsMagick에 stdin을 통해 어떻게 공급합니까?" 이 경우 실제로 위의 코드가 실제로 프로그램을 제대로 호출하지 않는다고 가정 할 때 실제로는 알 수 없습니다. 이제는 더 많이 생각해 보았습니다. stdin을 통해 * 두 개의 파일을 어떻게 공급할 계획입니까? 어쩌면 두 개의 파일을 대신 디스크에 써야할까요? ['tempnam'] (http://php.net/tempnam) (더 이상!) 편리 할 것입니다. – Charles