How to make Wordpress save_post_($post-type) hook works with multiple Custom Post Types

I have an issue related save_post_($post-type) hook and ACF gallery field.

I got this code snippet from somewhere. Let me explain: I created an ACF gallery field (acf-gallery is the field name) shown in Custom Post Type (cpt1 is the slug) then use this snippet to set the first image of this gallery as the featured image when saving just like what Woocommerce do.

But what if I want it to work with another Custom Post Type (let's say the slug is cpt2)? Can I use array( 'cpt1', 'cpt2' ) to replace cpt1? Is there a way to include multiple custom post types?


/* Set the first image generated by ACF gallery field as featured image */

add_action( 'save_post_cpt1', 'set_featured_image_from_gallery' );

function set_featured_image_from_gallery() {

    global $post;
    $post_id = $post->ID;

    $images = get_field('acf_gallery', $post_id, false);
    $image_id = $images[0];

    if ( $image_id ) {
        set_post_thumbnail( $post_id, $image_id );
    }
}

I edited this snippet using save-post hook according to comments below. But I don't know if it's valid. Can someone help?

/* Set the first image generated by ACF gallery field as featured image */

add_action( 'save_post', 'set_featured_image_from_gallery' );

function set_featured_image_from_gallery($post_id) {

    if (get_post_type($post_id) != array( 'cpt1', 'cpt2')) {
        return;
    }

    $has_thumbnail = get_the_post_thumbnail($post_id);

      if ( !$has_thumbnail ) {

        $images = get_field('acf_gallery', $post_id, false);
        $image_id = $images[0];

        if ( $image_id ) {
          set_post_thumbnail( $post_id, $image_id );
        }
      }
}

I usually use save_post hook, $post_id variable, get_post_type and in_array functions.

function set_featured_image_from_gallery($post_id)
{
    $included_cpts = array('cpt1', 'cpt2', 'cpt3');

    if (in_array(get_post_type($post_id), $included_cpts)) {

        $has_thumbnail = get_the_post_thumbnail($post_id);

        if (!$has_thumbnail) {

            $images = get_field('acf_gallery', $post_id, false);

            $image_id = $images[0];

            if ($image_id) {

                set_post_thumbnail($post_id, $image_id);
                
            }
        }
    }
}

add_action('save_post', 'set_featured_image_from_gallery');