如何从内存中将映像上传到PHP中的AWS S3? [英] How to upload image to AWS S3 in PHP from memory?
问题描述
所以我目前有一个使用AWS S3上载图像的上载系统.
So I currently have an upload system working using AWS S3 to upload images.
代码如下:
//Upload image to S3
$s3 = Aws\S3\S3Client::factory(array('key' => /*mykey*/, 'secret' => /*myskey*/,));
try {
$s3->putObject(array(
'Bucket' => "bucketname",
'Key' => $file_name,
'Body' => fopen(/*filelocation*/, 'r+')
));
} catch(Exception $e) {
//Error
}
此图片可以是jpeg或png,我想在上传之前将其转换为png.为此,我使用:
This image can be a jpeg or png, and I want to convert it to a png before uploading. To do this I use:
//This is simplified, please don't warn about transparency, etc.
$image = imagecreatetruecolor($width, $height);
imagecopyresampled($image, $source, 0, 0, 0, 0, etc.);
所以我在内存中有这个$image
对象.
So I have this $image
object in memory.
我想将其上传到S3,而不必将其保存在本地,先上传然后在本地删除;这个额外的步骤似乎毫无意义.但是我不知道如何直接上传此$ image对象.
I want to upload this to S3 without having to save it locally, upload it and then delete it locally; this extra step seems pointless. But I can't work out how to upload this $image object directly.
有什么想法可以做到吗?我以为fopen()会创建与imagecreatetruecolor()类似类型的对象,但是我尝试将$ image对象传递给它,但它不起作用-但是如果我使用fopen()在本地打开图像,它会起作用).
Any ideas how this would be done? I'd assumed fopen() would create an object of a similar type to imagecreatetruecolor(), but I've tried passing the $image object in and it doesn't work - whereas it does if I open an image locally with fopen().
推荐答案
您可以使用 查看全文