PHP on Webserver to send an image to another server and get back result

坚强是说给别人听的谎言 提交于 2021-02-08 03:48:00

问题


I am trying to implement all of this in I have a PHP file index.php running on a webserver (WS) on which clients upload files.

I have another server which is powerful enough (GPUs) to process these files. My use case is, clients upload images which are sent via a POST request to index.php. Now, it has to send the file to another server (GPU) and on GPU, another PHP file, say process.php has to take this image, process it.

So far, I think I can implement the above with PHP's cURL library.

My question is mostly about how do I get the processed image back to the client?

How do I make process.php send back the processed image to index.php and get it back to the client?

This must be a routine task but I would appreciate any help in implementing this.

code for index.php, I am storing the file on the webserver because I need to show a comparison (Before / After) once the processing is done. I have not yet implemented 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'];
?>

回答1:


Sorry for the wait. This is the script in WS that will receive the file from the client and will send it to GPU server. Notice I changed how the file is sent through curl (it was incorrect):

<?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;
}

And here's the script in the GPU server (this would be process.php):

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

This script will work on PHP 5.5+. If you're using an older version, we would have to change the way the file is sent in the WS script.

Hope this is what you're looking for.



来源:https://stackoverflow.com/questions/41250223/php-on-webserver-to-send-an-image-to-another-server-and-get-back-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!