2016-11-21 4 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' Будь ресурсом fopen –

+0

Typecast to saved_image в строку –

ответ

0

Хорошо, было бы полезно предложить вам принять ваше сообщение об ошибке и выполнить поиск в Google, так как есть множество HITS и множество объяснений.

Одно из предложений в сообщении об ошибке

[Body] должен быть Еореп ресурс

Какие подсказки, что вам нужно, чтобы спасти свое окончательное изображение и откройте его с помощью Еореп прохождения обрабатывать тело вместо $ saved_image.

Есть несколько ссылок, на которые вы можете найти информацию об этом в Google и в API AWS.

Смежные вопросы