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

Php 如何调整amazon S3存储桶的大小并上传图像?,php,exif,php-gd,Php,Exif,Php Gd,我正在尝试使用imagecopyresampled()调整图像大小,同时在上载到S3之前,使用imagecreatefromjpeg()从图像中删除EXIF数据。我不确定我遗漏了什么,因为图像上传了,但没有调整大小 $fileToUpload = 'file'; $tmp_name = $_FILES["$fileToUpload"]['tmp_name']; $file_name = ($_FILES["$fileToUpload"]["name"]); $sr

我正在尝试使用
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'],
]);

您正在取出图像并在其上运行
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'],
]);

那么您唯一的问题就是删除EXIF数据?通过删除其余的代码,您当然可以使这个问题变得更简单。S3与此问题无关。请参阅有关如何创建的信息。我想删除EXIF并将文件上载到S3,这会使其更加复杂。但您说“图像上载”,因此这不是问题的一部分。这就是困扰我的原因。它接收原始临时文件并上传,但在过程中没有修改。所以你唯一的问题是删除EXIF数据?通过删除其余的代码,您当然可以使这个问题变得更简单。S3与此问题无关。请参阅有关如何创建的信息。我想删除EXIF并将文件上载到S3,这会使其更加复杂。但您说“图像上载”,因此这不是问题的一部分。这就是困扰我的原因。它获取原始临时文件并将其上载,但在此过程中未对其进行修改。