WordPress.org

Forums

Can "add_attachment" be used to uploads thumbnails? (1 post)

  1. nouveller
    Member
    Posted 1 year ago #

    I'm using the "add_attachment" to sync images and video files to Amazon S3. But this doesn't seem to process any of the custom thumbnails. e.g. the original image will get sent to s3, but the 150x150 thumbnail won't get passed this same upload function. Any ideas would be greatly appreciated.

    add_action( 'add_attachment', function($attachment_ID) {
    
        $s3 = S3Client::factory();
        $s3_credentials = new Credentials('x', 'x');
        $s3->setCredentials($s3_credentials);
    
        // get the path to the uplaoded file
        $attachment = get_attached_file($attachment_ID);
        // filename
        $key = basename($attachment);
    
        $mimes = array(
            'webm'  => 'video/webm',
            'mp4'   => 'video/mp4',
            'flv'   => 'video/x-flv',
            'ogv'   => 'video/ogg',
            'jpg'   => 'image/jpeg',
            'png'   => 'image/png'
        );
    
        // check to see if it's an allowed file type
        $allowed_files = array('webm', 'mp4', 'flv', 'ogv', 'jpg', 'png');
        $filename = explode('.', $attachment);
        $file_ext = end($filename);
    
        if (in_array($file_ext, $allowed_files)) {
            $result = $s3->putObject(array(
                'ACL'           => 'public-read',
                'Bucket'        => 'bucket-name',
                'ContentType'   => $mimes[$file_ext],
                'Key'           => $key,
                'SourceFile'    => $attachment
            ));
        }
    });

Topic Closed

This topic has been closed to new replies.

About this Topic