2014-04-06 5 views
3

이미지 크기를 조정하고 자르기 스크립트가 있습니다. 즉석에서 내 amazon S3에 이미지를 업로드하고 싶습니다.PHP SDK를 사용하여 amazon S3에 크기 조정 이미지 업로드

문제는 소스 파일이 디스크 ($ filepath)의 직접 경로로 인식되지 않기 때문에 스크립트를 실행하려고 할 때 오류 메시지가 표시된다는 것입니다. 이 상황을 극복 할 생각이 있습니까?

Fatal error: Uncaught exception 'Aws\Common\Exception\InvalidArgumentException' with message 'You must specify a non-null value for the Body or SourceFile parameters.' in phar:///var/www/submit/aws.phar/Aws/Common/Client/UploadBodyListener.php:...

$myResizedImage = imagecreatetruecolor($width,$height); 
     imagecopyresampled($myResizedImage,$myImage,0,0,0,0, $width, $height, $origineWidth, $origineHeight); 

$myImageCrop = imagecreatetruecolor(612,612); 
     imagecopy($myImageCrop, $myResizedImage, 0,0, $posX, $posY, 612, 612); 

//Save image on Amazon S3 

    require 'aws.phar'; 

use Aws\S3\S3Client; 
use Aws\S3\Exception\S3Exception; 

$bucket = 'yol'; 
$keyname = 'image_resized';      
$filepath = $myImageCrop; 

// Instantiate the client. 
$s3 = S3Client::factory(array(
    'key' => 'private-key', 
    'secret' => 'secrete-key', 
    'region' => 'eu-west-1' 

    )); 

try { 
    // Upload data. 
    $result = $s3->putObject(array(
     'Bucket' => $bucket, 
     'Key' => $keyname, 
     'SourceFile' => $filepath, 
     'ACL' => 'public-read', 
     'ContentType' => 'image/jpeg' 
    )); 

    // Print the URL to the object. 
    echo $result['ObjectURL'] . "\n"; 
} catch (S3Exception $e) { 
    echo $e->getMessage() . "\n"; 
} 
+0

전체 코드를 게시 할 수 있습니까? 동일한 종류의 오류가 있습니다. – gnganpath

+0

무엇이 필요합니까? – casusbelli

답변

2

당신은 $myImageCrop = imagecreatetruecolor(...)에서 할당 $filepath에 업로드의 SourceFile을 설정하고 있습니다. 따라서 실제 경로가 아닙니다. GD 이미지 리소스입니다. S3에 직접 업로드 할 수는 없습니다.

당신은 파일에서 해당 이미지를 작성하거나해야합니다 (예를 들어 imagejpeg() + file_put_contents() 사용), 또는 (imagejpeg() 또는 이와 유사한부터 다시) 메모리에 데이터를 사용하여 업로드를 실행합니다.

+0

EC2 인스턴스에 내 이미지를 업로드 한 다음 S3에 내 이미지를 업로드해야한다는 의미입니까? – casusbelli

+0

나는 시도했다 :'$ filepath = imagejpeg ($ myImageCrop);'성공없이 – casusbelli

0
 require 'aws.phar'; 

use Aws\S3\S3Client; 
use Aws\S3\Exception\S3Exception; 

$bucket = 'kkkk'; 
$keyname = 'test'; 
// $filepath should be absolute path to a file on disk 

$newFielName = tempnam(null,null); // take a llok at the tempnam and adjust parameters if needed 
imagejpeg($myImageCrop, $newFielName, 100); // use $newFielName in putObjectFile() 

$filepath = $newFielName; 

// Instantiate the client. 
$s3 = S3Client::factory(array(
    'key' => 'jjj', 
    'secret' => 'kkk', 
    'region' => 'eu-west-1' 

    )); 

try { 
    // Upload data. 
    $result = $s3->putObject(array(
     'Bucket' => $bucket, 
     'Key' => $keyname, 
     'SourceFile' => $filepath, 
     'ACL' => 'public-read', 
     'ContentType' => 'image/jpeg' 
    )); 

    // Print the URL to the object. 
    echo $result['ObjectURL'] . "\n"; 
} catch (S3Exception $e) { 
    echo $e->getMessage() . "\n"; 
} 
3

이미지 리소스를 이미지 데이터가 포함 된 실제 문자열로 변환해야합니다. 이 기능을 사용하여 다음을 달성 할 수 있습니다.

function image_data($gdimage) 
{ 
    ob_start(); 
    imagejpeg($gdimage); 
    return(ob_get_clean()); 
} 
+0

파일을 저장하고 싶지 않은 사람들을위한 좋은 해결책. 'SourceFile' 대신'Body '=> image_data ($ img)'를 사용하십시오. – hellohellosharp