2012-03-22 2 views
1

proc_open() PHP 함수를 사용할 때 프로세스에 파이프되는 스트림을 사용하는 데 문제가있는 것 같습니다.여러 입력 스트림에서 php proc_open() 사용. 응답 중지 방지

내가 시작하는 프로세스는 단순히 ImageMagick 유틸리티를 변환하여 서로 위에 3 개의 이미지를 작성하는 것입니다.

$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); 

먼저 내가 변환을 실행하고 : 단 하나의 입력 스트림 (STDIN) 사용하면 변수는 변환 프로그램이 잘 작동하고과 같이 변수에 저장 될 수있는 출력을 반환 스트림으로 덤프됩니다 출력을 $ 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 구문은 fd : N과 같은 대체 스트림을 지정합니다. 여기서 N은 proc_open()을 통해 생성되는 스트림의 번호입니다). 그러나 나는 입력 스트림에 쓰거나 STDOUT에서 읽는 것을 잘못 처리하는 것으로 보이는데,이 결과는 종료되지 않은 채 멈추는 프로세스의 출력을 unflushed 할 때 가장 많이 발생합니다.

$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 하나의 이미지를 생성 연결하는이었다 빈/잘못된 데이터

답변

1

에 오류가 발생합니다으로 3 개 & 4 파이프 '내용을 전송할 수없는 것 PHP 변수. 그래서 각 이미지 출력은 다음 이미지로 연결됩니다. 나중에 proc_open()이 호출 될 때 변환 입력 스트림 (입력 파일 데이터를 보유 할 연결 대상 이미지 var)에 STDIN 만 지정하십시오. 내가 사용하고있는 형식은 PNG이며 스택 된 이미지와 잘 작동하는 것 같습니다. PNG를 제공 할 때마다 : - 입력을위한 변환 명령에서 다음 이미지가 끌어와 입력으로 사용됩니다. 이렇게하면 단일 변수에서 3 개의 이미지 모두에 대한 이미지 데이터를 제공 할 수 있습니다.

이 솔루션은 Imagemagick에서 가능한 것으로 문서화 된 번호가 매겨진 파일 설명자 fd : N의 대안입니다. 그것은 작업을 수행하지만, 왜 내가 proc_open()으로 설정하고 Imagemagick convert에 제공하는 여분의 입력 파이프가 작동하지 않는 지에 대한 질문을합니다.

  $khh = BIN_PATH.DIRECTORY_SEPARATOR.'convert '; 
     $khh .= '     -size '.$imageSize.' '; 
     $khh .= '     xc:\'rgb(230, 225, 50)\' '; 
     $khh .= '     -gravity center '; 
     $khh .= '     - '; 
     $khh .= '     -alpha Off '; 
     $khh .= '     -compose CopyOpacity '; 
     $khh .= '     -composite '; 
     $khh .= '     -trim '; 
        $khh .= '     png:- '; 

     $chighlight = ''; 
     $descriptorspec = array(
       0 => array("pipe", "r"), 
       1 => array("pipe", "w") 
     ); 
     $process = proc_open($khh, $descriptorspec, $pipes); 

     if (is_resource($process)) { 
      fwrite($pipes[0], $ctext_opacity); 
      fclose($pipes[0]); 

      while (!feof($pipes[1])) { 
       $chighlight .= fgets($pipes[1]); 
      } 

      fclose($pipes[1]); 

      $return_value = proc_close($process); 
     } 

.................................................. .........................................

  $khh = BIN_PATH.DIRECTORY_SEPARATOR.'convert '; 
     $khh .= '     -size '.$imageSize.' '; 
     $khh .= '     xc:'.$fontColor.' '; 
     $khh .= '     -gravity center '; 
     $khh .= '     - '; 
     $khh .= '     -alpha Off '; 
     $khh .= '     -compose CopyOpacity '; 
     $khh .= '     -composite '; 
     $khh .= '     -trim '; 
     if($_GET['ctr']==='3') { 
      $khh .= '     png:- '; 
     } else { 
      $khh .= '     png:- '; 
     } 
     $ctext = ''; 
     $descriptorspec = array(
       0 => array("pipe", "r"), 
       1 => array("pipe", "w") 
     ); 
     $process = proc_open($khh, $descriptorspec, $pipes); 

     if (is_resource($process)) { 
      fwrite($pipes[0], $ctext_opacity); 
      fclose($pipes[0]); 

      while (!feof($pipes[1])) { 
       $ctext .= fgets($pipes[1]); 
      } 

      fclose($pipes[1]); 

      $return_value = proc_close($process); 
     } 

.. .................................................. ................................

  $khh = BIN_PATH.DIRECTORY_SEPARATOR.'convert '; 
     $khh .= '     -size '.$imageSize.' '; 
     $khh .= '     xc:\'rgb(50, 85, 20)\' '; 
     $khh .= '     -gravity center '; 
     $khh .= '     - '; 
     $khh .= '     -alpha Off '; 
     $khh .= '     -compose CopyOpacity '; 
     $khh .= '     -composite '; 
     $khh .= '     -trim '; 
      $khh .= '     png:- '; 
     $cshade = ''; 
     $descriptorspec = array(
       0 => array("pipe", "r"), 
       1 => array("pipe", "w") 
     ); 
     $process = proc_open($khh, $descriptorspec, $pipes); 

     if (is_resource($process)) { 
      fwrite($pipes[0], $ctext_opacity); 
      fclose($pipes[0]); 

      while (!feof($pipes[1])) { 
       $cshade .= fgets($pipes[1]); 
      } 

      fclose($pipes[1]); 

      $return_value = proc_close($process); 
     } 

........... .................................................. .......................

  $khh = BIN_PATH.DIRECTORY_SEPARATOR.'convert '; 
     $khh .= '     -size '.$sizeX.'x'.$sizeY; 
     $khh .= '     xc:transparent '; 
     $khh .= '     -gravity center '; 
     $khh .= '     png:- -geometry -'.$i.'-'.$i.' -composite '; 
     $khh .= '     png:- -geometry +'.$i.'+'.$i.' -composite '; 
     $khh .= '     png:- -composite '; 
       $khh .= '     png:- '; 

     $ctext_deboss = ''; 
      $descriptorspec = array(
       0 => array("pipe", "r"), 
       1 => array("pipe", "w") 
     ); 
     $process = proc_open($khh, $descriptorspec, $pipes); 

     if (is_resource($process)) { 
      fwrite($pipes[0], $cshade.$chighlight.$ctext); 
      fclose($pipes[0]); 

      while (!feof($pipes[1])) { 
       $ctext_deboss .= fgets($pipes[1]); 
      } 

      fclose($pipes[1]); 

      $return_value = proc_close($process); 
     } 

............................................. .......................................