在缩略图短代码中添加帖子ID请求

I use the following script to create a thumbnail shortcode, but this will only work at a loop or inside the post.

/* Thumb Shortcode */
function my_img() {
if (has_post_thumbnail() ) {
    $image_id = get_post_thumbnail_id();  
    $image_url = wp_get_attachment_image_src($image_id,'medium');  
    $image_url = $image_url[0]; 
    $result = '<img src="'.$image_url.'" class="my_img" />';
    return $result;
}
return;
}
add_shortcode ('my_img', 'my_img');

Is there any simple way to modify it, so that I can use it everywhere with a post ID?

Like this:

<?php echo do_shortcode('[my_img post="100"]'); ?>

This function will use shortcode_atts to get the post shortcode attribute if it exist, if not it will use the thumb from the loop:

function my_img_func($atts) {
    $atts = shortcode_atts(array(
        'post' => null
    ), $atts, 'my_img');
    if($atts['post'] != null) {
        if (has_post_thumbnail((int) $atts['post']) ) {
            $image_id = get_post_thumbnail_id((int) $atts['post']);  
            $image_url = wp_get_attachment_image_src($image_id,'medium');  
            $image_url = $image_url[0]; 
            $result = '<img src="'.$image_url.'" class="my_img" />';
            return $result;
        }
    } else {
        if (has_post_thumbnail() ) {
            $image_id = get_post_thumbnail_id();  
            $image_url = wp_get_attachment_image_src($image_id,'medium');  
            $image_url = $image_url[0]; 
            $result = '<img src="'.$image_url.'" class="my_img" />';
            return $result;
        }
    }
    return '';
}
add_shortcode ('my_img', 'my_img_func');