How to get all images in Media Gallery with pagination?

EDIT: I didn’t read your code properly, sorry!

You’re using get posts which will only retrieve posts. To get all the images from the media library you’ll need to use WP_query.

$query_images_args = array(
    'post_type' => 'attachment',
    'post_mime_type' =>'image',
    'post_status' => 'inherit',
    'posts_per_page' => -1,
);

$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
    $images[]= wp_get_attachment_url( $image->ID );
}

This code will put all the URLs of images from the media library into the $images array.

ref: https://wpeden.com/tipsntuts/list-all-images-at-media-gallery-in-your-wordpress-site/