我有一个脚本,它会重新调整大小并裁剪图像,我想在运行中将图像上传到我的亚马逊S3上。
问题是当我尝试运行脚本时收到错误消息,因为我猜源文件不被识别为来自磁盘的直接路径($ filepath)。你有什么想法来解决这个问题吗?
致命错误:带有消息'的未捕获异常'Aws \ Common \ Exception \ InvalidArgumentException'您必须在phar:/// var中为Body或SourceFile参数指定非空值。' /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 :(得分:3)
您需要将图像资源转换为包含图像数据的实际字符串。您可以使用此功能来实现此目的:
function image_data($gdimage)
{
ob_start();
imagejpeg($gdimage);
return(ob_get_clean());
}
答案 1 :(得分:2)
您要将上传的SourceFile
设置为$filepath
,该$myImageCrop = imagecreatetruecolor(...)
是从imagejpeg()
分配的。因此,它实际上并不是一条路径 - 它是GD图像资源。您无法直接将这些内容上传到S3。
您需要将该图像写入文件(使用例如file_put_contents()
+ imagejpeg()
),或使用内存中的数据运行上传(同样,来自{{1}}或类似的。)
答案 2 :(得分: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";
}