How to get ID of images used in gallery?

Are you writing a template? A filter in functions.php or a plugin? A straightforward method could be using get_post_gallery with the second argument set to false, so that it return the object rather than the html.

if ( get_post_gallery() ) :

    //Get the gallery object
    $gallery = get_post_gallery( get_the_ID(), false );

    //Form an array with the found ids
    $gallery_attachment_ids = explode( ',', $gallery['ids'] );

endif;