繁体   English   中英

如何为 amazon S3 存储桶调整大小并上传图像?

[英]How can I resize and upload an image for the amazon S3 bucket?

我正在尝试使用imagecopyresampled()调整图像大小,同时在上传到 S3 之前使用imagecreatefromjpeg()从图像中删除 EXIF 数据。 我不确定我错过了什么,因为图像会上传但不会调整大小。

$fileToUpload = 'file';
$tmp_name = $_FILES["$fileToUpload"]['tmp_name'];
$file_name        = ($_FILES["$fileToUpload"]["name"]);

        $src = imagecreatefromjpeg($tmp_name);
        list($width_orig, $height_orig) = getimagesize($tmp_name);
        $width = 50; //px
        $height = 50; //px
        $image_p = imagecreatetruecolor($width, $height); // resize image
        imagecopyresampled($image_p, $src, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);//changes original image


                $config = [
                    's3-access' => [
                        'key' => 'awsAccessKey',
                        'secret' => 'awsSecretKey',
                        'bucket' => 'awsBucket',
                        'region' => 'us-east-1', 
                        'version' => 'latest',
                        'acl' => 'public-read',
                        'private-acl' => 'private'
                    ]
                ];     //# initializing s3
                $s3 = Aws\S3\S3Client::factory([
                    'credentials' => [
                        'key' => $config['s3-access']['key'],
                        'secret' => $config['s3-access']['secret']
                    ],
                    'version' => $config['s3-access']['version'],
                    'region' => $config['s3-access']['region']
                ]);
                $request_status = $s3->putObject([
                    'Bucket' => $config['s3-access']['bucket'],
                    'Key' => $file_name,
                    'SourceFile' => $tmp_name,
                    'ACL' => $config['s3-access']['acl']
                ]);

您正在拉出图像并在其上运行imagecopyresampled ,但您从未将新图像写入磁盘,而只是上传原始文件。

尝试这个:

$fileToUpload = 'file';
$tmp_name = $_FILES["$fileToUpload"]['tmp_name'];
$file_name = $_FILES["$fileToUpload"]["name"];

list($width_orig, $height_orig) = getimagesize($tmp_name);
$width = 50;
$height = 50;
// get the old image from disk
$src = imagecreatefromjpeg($tmp_name);
// create a new image
$image_p = imagecreatetruecolor($width, $height);
// resample the old image into the new image
imagecopyresampled($image_p, $src, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// clean up
imagedestroy($src);
// save the new image to disk
imagejpeg($image_p, $tmp_name);
// clean up
imagedestroy($image_p);

$config = [
    's3-access' => [
        'key' => 'awsAccessKey',
        'secret' => 'awsSecretKey',
        'bucket' => 'awsBucket',
        'region' => 'us-east-1', 
        'version' => 'latest',
        'acl' => 'public-read',
        'private-acl' => 'private',
    ]
];

$s3 = Aws\S3\S3Client::factory([
    'credentials' => [
        'key' => $config['s3-access']['key'],
        'secret' => $config['s3-access']['secret'],
    ],
    'version' => $config['s3-access']['version'],
    'region' => $config['s3-access']['region'],
]);

$request_status = $s3->putObject([
    'Bucket' => $config['s3-access']['bucket'],
    'Key' => $file_name,
    'SourceFile' => $tmp_name,
    'ACL' => $config['s3-access']['acl'],
]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM