2016-11-21 36 views
0

リモートイメージを取得し、サイズを変更してAWS S3にアップロードしようとしていますが、解決方法がわかりません。リモートイメージの取得と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. 

私が間違っているのは何?

+0

'body'パラメータリストfooenリソース –

+0

文字列にsaved_imageへの型変換 –

答えて

0

多くのヒットや説明があるので、エラーメッセージを受け取り、Googleで検索を実行することをお勧めします。エラーメッセージの提案の

一つ

[ボディ]あなたが合格fopenを使って、最終的な画像を保存し、それを開いている必要があることをヒントにfopenリソース

でなければなりません$ saved_imageの代わりにbodyにハンドルします。

これについてGoogleとAWS APIで尋ねることができる参考資料がいくつかあります。

関連する問題