Custom RSS causing timeout for website

As @TomJNowell mentioned it’s weird to use get_headers() for this. It would be better to remove the $img variable entirely and just use strlen() in your enclosure to get the length of the string in bytes:

<enclosure url="<?php
    if ( has_post_thumbnail( $post->ID ) ) {
        the_post_thumbnail_url();
    }
    ?>" length="<?php 
        if ( $image ) {
            echo strlen( $image[0] );
        }?>" type="image/jpg" />