Display images of related posts

I’m fetching the attachment ID by get_post_thumbnail_id function which is taking the post ID. After getting the attachment ID of that post, I am passing this attachment ID to wp_get_attachment_image_src function and getting the image details.

So I am rewriting your foreach loop like this way

foreach($related as $r) {
    $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($r->ID), 'thumbnail', true );
    $img= '';

    if( is_array( $thumbnail ) ) {
        $img = sprintf('<img src="https://wordpress.stackexchange.com/questions/327035/%s" width="https://wordpress.stackexchange.com/questions/327035/%s" height="https://wordpress.stackexchange.com/questions/327035/%s" alt="https://wordpress.stackexchange.com/questions/327035/%s"> ', $thumbnail[0], $thumbnail[1], $thumbnail[2], $r->post_title);
    }

    $retval .= '<li><a class="link-related" href="'.get_permalink($r->ID).'" title="'.wptexturize($r->post_title).'">'. $img . wptexturize($r->post_title).'</a></li>';
}