phpexifphp-gd

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


I am trying to resize an image with imagecopyresampled() and at the same time remove the EXIF data from the image with imagecreatefromjpeg() before uploading to S3. I am not sure what I am missing, because the image uploads but doesn't resize.

$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']
                ]);

Solution

  • You're pulling the image out and running imagecopyresampled on it, but you never write the new image to disk, and just upload the original file.

    Try this:

    $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'],
    ]);