2016-12-20 5 views
2

私はこれをすべて実装しようとしています クライアントがファイルをアップロードするWebサーバー(WS)上でPHPファイルindex.phpを実行しています。Webサーバー上のPHPが別のサーバーに画像を送信して結果を返す

私はこれらのファイルを処理するのに十分強力な別のサーバー(GPU)を持っています。 私の使用例は、クライアントがPOSTリクエストを介してindex.phpに送信された画像をアップロードする場合です。さて、別のサーバ(GPU)にファイルを送信し、別のPHPファイルであるGPUでprocess.phpがこのイメージを処理しなければならないとします。

これまでのところ、私はPHPのcURLライブラリで上記を実装できると思います。

私の質問は、処理されたイメージをどのようにしてクライアントに戻すのですか?

process.phpは処理された画像をindex.phpに送り返してクライアントに戻すにはどうすればよいですか?

これは日常的な作業でなければなりませんが、これを実装する際には助けてください。

コードindex.phpの場合、処理が完了したら比較(前/後)を表示する必要があるため、ファイルをWebサーバーに保存しています。私はまだ実装していませんprocess.php

<?php 
$ds = DIRECTORY_SEPARATOR; 
$storeFolder = 'uploads'; 

if (!empty($_FILES)) { 
    $tempFile = $_FILES['file']['tmp_name']; 
    $targetPath = dirname(__FILE__) . $ds. $storeFolder . $ds; 
    $targetFile = $targetPath. $_FILES['file']['name']; 
    move_uploaded_file($tempFile,$targetFile); 
} 

function cURLcheckBasicFunctions() { 
    if(!function_exists("curl_init") && 
     !function_exists("curl_setopt") && 
     !function_exists("curl_exec") && 
     !function_exists("curl_close")) return false; 
    else return true; 
} 

if(!cURLcheckBasicFunctions()) 
{ echo "UNAVAILABLE: cURL Basic Functions"; } 

// $url = "129.132.102.52/process.php"; 
$url = "dump_test.php"; 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL,$url); 
curl_setopt($ch,CURLOPT_POST, 1); 

$fp = fopen($targetFile, "w"); 

curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$reply = curl_exec($ch); 
curl_close($ch); 
fclose($fp); 

echo $_FILES['file']['name']; 
?> 
+0

なぜGPUサーバが処理されたイメージで応答するのはなぜですか?この方法であなたは '$ reply = curl_exec($ ch);'でイメージを取得します。 – Parziphal

+0

これは 'POST'要求として再度行われるべきでしょうか?画像を送るために 'process.php'をどうすればいいのか分かりません。 –

+0

クライアントは画像をWSにアップロードします.WSは、画像でGPUにカールを要求し、GPUが応答するのを待ちます。 GPUは画像と共にリクエストを受け取り、画像を処理するために 'process.php'を必要とし、処理が終了すると、最終画像をエコーし​​て終了します。したがって、処理されたイメージはリクエストレスポンスとして送信されるので、イメージを保存したり、クライアントに返すなど、WSのカールリクエストの結果として取得します。これは意味がありますか? heh。あなたが望むなら、私はその絵を手助けするスニペットを書くことができました。 – Parziphal

答えて

2

申し訳ありません。 これはWSのスクリプトで、クライアントからファイルを受け取り、GPUサーバーに送信します。 (それは間違っていた)私は、ファイルがカールを介して送信される方法を変更注意してください:

<?php 

$ds = DIRECTORY_SEPARATOR; 
$storeFolder = 'uploads'; 

if (!empty($_FILES)) { 
    $tempFile = $_FILES['file']['tmp_name']; 
    $targetPath = dirname(__FILE__) . $ds. $storeFolder . $ds; 
    $targetFile = $targetPath. $_FILES['file']['name']; 
    move_uploaded_file($tempFile,$targetFile); 
} 

if(!cURLcheckBasicFunctions()) 
{ echo "UNAVAILABLE: cURL Basic Functions"; } 

// $url = "129.132.102.52/process.php"; 
$url = "dump_test.php"; 

$file = new CURLFile($tempFile); 

$ch = curl_init(); 

curl_setopt($ch,CURLOPT_URL, $url); 
curl_setopt($ch,CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, [ 
    'file' => $file, 
]); 

/** 
* As you can see in the script below, the GPU will echo the processed 
* file and we will capture it here. 
*/ 
$processedImage = curl_exec($ch); 
curl_close($ch); 

/** 
* And now you can do anything with the processed file. 
* For example, let's save it into a file. 
*/ 
file_put_contents('processed_image.jpg', $processedImage); 

function cURLcheckBasicFunctions() { 
    if(!function_exists("curl_init") && 
     !function_exists("curl_setopt") && 
     !function_exists("curl_exec") && 
     !function_exists("curl_close")) return false; 
    else return true; 
} 

そして、ここでは(これはprocess.phpだろう)GPUサーバーでスクリプトです:

<?php 

$tempFile = $_FILES['file']['tmp_name']; 

// Here you would process the file.... 

// Let's pretend you have the full path to the processed image in the $processedFilePath var. 
// Now we will output the processed file contents so the WS server will receive it. 

// The header isn't necessary but let's put it. 
header('Content-Type: image/jpg'); 

echo file_get_contents($processedFilePath); 

このスクリプトは、上で動作しますPHP 5.5+。古いバージョンを使用している場合は、WSスクリプトでファイルを送信する方法を変更する必要があります。

お探しのものがこれです。

関連する問題