The Thumbnail aspect Ratio Issue

I have used thumbnail like this in my wordpress theme’s one template →

<?php the_post_thumbnail( 'medium' ); ?>

In the browser it is rendering like this →

    class="attachment-medium size-medium wp-post-image" 
    srcset=" 300w, 640w" 
    sizes="(max-width: 300px) 100vw, 300px"

My first question is how to put height = auto is there any function that can help us to achieve this?
such as responsive-img

In short, I am asking should I control the width through the CSS or WordPress gives some function to do this?

Solutions Collecting From Web of "The Thumbnail aspect Ratio Issue"

If you want to remove the height value from your img URL, you can use this function:

add_filter( 'post_thumbnail_html', 'remove_thumbnail_height', 10, 5 );
function remove_thumbnail_height( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
    $html = preg_replace( '/height=\"\d*\"/', "", $html );
    return $html;

This will replace the height with an empty value. Note that you can’t use Auto as a value for the height property. It won’t be validated by w3 validator. However, you can set the height to auto in your CSS:

.wp-post-image {
    height: auto;

You can use something like this,

 $image_thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'thumbnail');
 $image_medium = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium');
 $image_large = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'large');

 <img class="img-responsive" src="<?php echo $image_thumb[0]?> srcset="<?php echo $image_large[0];?>  1240w, <?php echo $image_medium[0];?>  620w, <?php echo $image_small[0];?>   310w" sizes="(min-width: 41.25em) 41.25em, 100vw" />;

If you are not using bootstrap framework, in style sheet put

   width: 100%;