You need to split the get_post_gallery ids
string into an array before you can loop through it.
$parent = get_post_field( 'post_parent', get_the_ID() );
$gallery = get_post_gallery($parent, false);
$ids = explode(',', $gallery[ 'ids' ]);
foreach($ids as $attachment_id) {
$img_src = wp_get_attachment_image_url($attachment_id, 'medium');
$img_srcset = wp_get_attachment_image_srcset($attachment_id, 'medium');
$title = get_the_title($attachment_id);
?>
<a href="https://wordpress.stackexchange.com/questions/212996/<?php echo get_permalink($attachment_id); ?>">
<img src="<?php echo esc_url($img_src); ?>"
srcset="https://wordpress.stackexchange.com/<?php echo esc_attr($img_srcset); ?>"
sizes="auto" alt="<?php echo esc_attr($title); ?>"></a>
<?php
}