获取远程映像并上载到AWS S3

时间:2016-11-21 02:53:28

标签: php

我正在尝试获取远程图像,调整其大小并将其上传到AWS S3,我收到的错误是我不知道如何解决。

这是我的代码:

//Fetch and resize remote image
$max_width = 800;
$remote_img = file_get_contents("https://commons.wikimedia.org/wiki/Stovall_House#/media/File:Tampa_Stovall_House_gazebo01.jpg");
$im = imagecreatefromstring($remote_img);
$remote_img_width = imagesx($im);
$remote_img_height = imagesy($im);
if($remote_img_width<=$max_width){
    $saved_img_width = $remote_img_width;
    $saved_img_height = $remote_img_height;
    } else {
    $saved_img_width = $max_width;
    $ratio = $max_width/$remote_img_width;
    $saved_img_height = $remote_img_height * $ratio;
    }
$saved_image = imagecreatetruecolor($saved_img_width, $saved_img_height);
imagecopyresized($saved_image , $im, 0, 0, 0, 0, $saved_img_width, $saved_img_height, $remote_img_width, $remote_img_height);

//Upload to AWS S3
require 'aws/aws-autoloader.php';
use Aws\S3\S3Client;
$s3 = new Aws\S3\S3Client([
    'version'     => 'latest',
    'region'      => 'us-east-1',
    'credentials' => [
        'key'    => 'ABCD1234',
        'secret' => 'ABCD1234'
        ]
    ]);
$result = $s3->putObject(array(
        'Bucket' => 'aws-website-virulous-bebmb',
        'Key'    => 'ABCD1234',
        'Body'   => $saved_image,
        'ACL'    => 'public-read'
    ));

// Get the URL the object can be downloaded from and display
$image_url = $result['ObjectURL'];
echo "The image can be seen at <a href='" . $image_url . "' target='_new'>" . $image_url . "</a>";

//Clean up
imagedestroy($saved_image); 
imagedestroy($im);

这是我得到的错误:

Fatal error: Uncaught InvalidArgumentException: Found 1 error while validating the input provided for the PutObject operation: [Body] must be an fopen resource, a GuzzleHttp\Stream\StreamInterface object, or something that can be cast to a string.

我做错了什么?

1 个答案:

答案 0 :(得分:0)

建议你提出错误信息并在Google上进行搜索会更有利,因为有大量的HITS和许多解释。

错误消息中的一条建议

  

[Body]必须是fopen资源

您需要保存最终图像的提示,并使用fopen将句柄传递给body而不是$ saved_image来打开它。

您可以在Google和AWS API中找到一些有关此内容的参考资料。