2

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'];
?>
6
  • Why not just make the GPU server respond with the processed image? This way you would get the image in $reply = curl_exec($ch);. Commented Dec 20, 2016 at 20:00
  • Should that be done as a POST request again? I am not sure of how I could get the process.php to send the image. Commented Dec 20, 2016 at 20:04
  • Client uploads image to WS, which makes a curl request to GPU with the image, and waits for GPU to respond. GPU gets the request with the image, requires process.php to process the image, and when it's done with the process, it echoes the final image and exits. The processed image is therefore sent as the request response, so you will get it as the result of the curl request in WS, where you can store the image, send it back to the client, or etc. Does this makes sense? heh. I could write snippets to help with the picture if you want. Commented Dec 20, 2016 at 20:11
  • I got all that except the it echoes the final image part. Would that be like echo <img src=...? In any case, if you could write the snippets as an answer, I'd happily accept it! Commented Dec 20, 2016 at 20:34
  • Do you use Imagick or GD2 to process the image? Commented Dec 20, 2016 at 20:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.