2012-03-22 46 views
1

好像我在使用proc_open()php函數時使用流傳輸到進程的流時遇到問題。在多個輸入流中使用php proc_open()。防止掛起

我開始的過程只是轉換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(); 
    } 
} 

我似乎無法作爲轉換引發錯誤空/不正確的數據

回答

1

我沉澱在溶液以連接3生成的圖像中一個轉移3個& 4個管的內容PHP變量。 因此,每個圖像輸出連接到下一個。稍後,當調用proc_open()時,只爲轉換輸入流分配STDIN(它將保存輸入文件數據,即我們的連接圖像var)。我使用的格式是PNG,它似乎可以很好地處理堆疊的圖像。每次提供png時: - 在輸入的convert命令中,下一個圖像將被拉出並用作輸入。這樣您就可以從一個變量中爲所有3張圖像提供圖像數據。

該解決方案只是編號文件描述符fd:N的一種替代方法,它在Imagemagick中被記錄爲可能。它確實完成了任務,但是爲什麼用proc_open()設置並提供給Imagemagick轉換器的額外輸入管道不起作用。

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

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