Apply several conditions when uploading an avatar image

I had a problem when uploading the avatar image, which I fixed: link

Now I want some conditions to apply when uploading an avatar image, In the one user avatar plugin settings, I ticked the avatar image size change and set its size to 32×32.

  1. Only allow uploading avatar images that are equal in width and length, exp : 300×300, 1000×1000
  2. When the avatar image is uploaded, it will remove the original size and replace it with a scaled 32×32 size. I want to keep the original size, actually the avatar image has 2 sizes : original and 32×32
  3. When the avatar image is changed, all 2 sizes are removed and the avatar image is replaced with the original 2 sizes and 32×32.

This is my code :

function customize_wpua_action_process_option_update( $user_id ) {

    global  $blog_id,
            $post,
            $wpdb,
            $wp_user_avatar,
            $wpua_force_file_uploader,
            $wpua_resize_crop,
            $wpua_resize_h,
            $wpua_resize_upload,
            $wpua_resize_w,
            $wpua_admin;

    // Check if user has publish_posts capability
    if ( $wp_user_avatar->wpua_is_author_or_above() && ! $wpua_force_file_uploader ) {
        $wpua_id = isset( $_POST['wp-user-avatar'] ) ? absint( $_POST['wp-user-avatar'] ) : 0;

        // Remove old attachment postmeta
        delete_metadata( 'post', null, '_wp_attachment_wp_user_avatar', $user_id, true );

        // Create new attachment postmeta
        add_post_meta( $wpua_id, '_wp_attachment_wp_user_avatar', $user_id );

        // Update usermeta
        update_user_meta( $user_id, $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar', $wpua_id );
    } else {
        // Remove attachment info if avatar is blank
        if ( isset( $_POST['wp-user-avatar'] ) && empty( $_POST['wp-user-avatar'] ) ) {
            // Delete other uploads by user
            $q = array(
                'author'         => $user_id,
                'post_type'      => 'attachment',
                'post_status'    => 'inherit',
                'posts_per_page' => '-1',
                'meta_query'     => array(
                    array(
                        'key'     => '_wp_attachment_wp_user_avatar',
                        'value'   => "",
                        'compare' => '!='
                    ),
                ),
            );

            $avatars_wp_query = new WP_Query( $q );

            while( $avatars_wp_query->have_posts() ) {
                $avatars_wp_query->the_post();

                wp_delete_attachment( $post->ID );
            }

            wp_reset_query();

            // Remove attachment postmeta
            delete_metadata( 'post', null, '_wp_attachment_wp_user_avatar', $user_id, true );

            // Remove usermeta
            update_user_meta( $user_id, $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar', '' );
        }

        // Create attachment from upload
        if ( ! empty( $_FILES['wpua-file'] ) ) {
            $file = $_FILES['wpua-file'];
            $name = isset( $file['name'] ) ? sanitize_file_name( $file['name'] ) : '';
            $type = isset( $file['type'] ) ? sanitize_mime_type( $file['type'] ) : '';
            $file = wp_handle_upload( $file, array(
                'test_form' => false,
            ) );

            if ( isset( $file['url'] ) ) {
                if ( ! empty( $type ) && preg_match( '/(jpe?g|gif|png)$/i' , $type ) ) {
                    // Resize uploaded image
                    if ( 1 == (bool) $wpua_resize_upload ) {
                        // Original image
                        $uploaded_image = wp_get_image_editor( $file['file'] );

                        // Check for errors
                        if ( ! is_wp_error( $uploaded_image ) ) {
                            // Resize image
                            $uploaded_image->resize( $wpua_resize_w, $wpua_resize_h, $wpua_resize_crop );

                            // Save image
                            $uploaded_image->save( $file['filee'] );
                        }
                    }

                    // Break out file info
                    $name_parts = pathinfo( $name );
                    $name       = trim( substr( $name, 0, -( 1 + strlen( $name_parts['extension'] ) ) ) );
                    $url        = $file['url'];
                    $file       = $file['file'];
                    $title      = $name;

                    // Use image exif/iptc data for title if possible
                    if ( $image_meta = @wp_read_image_metadata( $file ) ) {
                        if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
                            $title = $image_meta['title'];
                        }
                    }

                    // Construct the attachment array
                    $attachment = array(
                        'guid'           => $url,
                        'post_mime_type' => $type,
                        'post_title'     => $title,
                        'post_content'   => '',
                    );

                    // This should never be set as it would then overwrite an existing attachment
                    if ( isset( $attachment['ID'] ) ) {
                        unset( $attachment['ID'] );
                    }

                    // Save the attachment metadata
                    $attachment_id = wp_insert_attachment( $attachment, $file );

                    if ( ! is_wp_error( $attachment_id ) ) {
                        // Delete other uploads by user
                        $q = array(
                            'author'         => $user_id,
                            'post_type'      => 'attachment',
                            'post_status'    => 'inherit',
                            'posts_per_page' => '-1',
                            'meta_query'     => array(
                                array(
                                    'key'     => '_wp_attachment_wp_user_avatar',
                                    'value'   => '',
                                    'compare' => '!=',
                                ),
                            ),
                        );

                        $avatars_wp_query = new WP_Query( $q );

                        while ( $avatars_wp_query->have_posts() ){
                            $avatars_wp_query->the_post();

                            wp_delete_attachment($post->ID);
                        }

                        wp_reset_query();

                        wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );

                        // Remove old attachment postmeta
                        delete_metadata( 'post', null, '_wp_attachment_wp_user_avatar', $user_id, true );

                        // Create new attachment postmeta
                        update_post_meta( $attachment_id, '_wp_attachment_wp_user_avatar', $user_id );

                        // Update usermeta
                        update_user_meta( $user_id, $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar', $attachment_id );
                    }
                }
            }
        }
    }
} add_action( 'woocommerce_save_account_details', 'customize_wpua_action_process_option_update' );