Web服务器上的PHP将图像发送到另一台服务器并获取结果 [英] PHP on Webserver to send an image to another server and get back result

查看:40
本文介绍了Web服务器上的PHP将图像发送到另一台服务器并获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在以下方面实现所有这些功能我有一个在客户端上载文件的Web服务器(WS)上运行的PHP文件 index.php .

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.

我有另一台服务器,该服务器功能强大(GPU),可以处理这些文件.我的用例是,客户端将通过 POST 请求发送的图像上传到 index.php .现在,它必须将文件发送到另一个服务器(GPU),并且在GPU上,另一个PHP文件,例如 process.php 必须获取此图像,然后对其进行处理.

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.

到目前为止,我想我可以使用PHP的 cURL 库实现以上功能.

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?

如何使 process.php 发送回 index.php 处理过的图像并将其返回给客户端?

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,我将文件存储在Web服务器上,因为一旦处理完成,我需要显示一个比较(之前/之后).我尚未实现 process.php

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'];
?>

推荐答案

抱歉,等待.这是WS中的脚本,它将从客户端接收文件并将其发送到GPU服务器.注意,我更改了通过curl发送文件的方式(不正确):

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

这是GPU服务器中的脚本(这是 process.php ):

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

此脚本将在PHP 5.5+上运行.如果您使用的是旧版本,则必须更改WS脚本中文件的发送方式.

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.

希望这就是您想要的.

这篇关于Web服务器上的PHP将图像发送到另一台服务器并获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆