How to use wp_get_attachment_metadata for thumbnails

The structure would be something like this.

  • Check if you have a thumbnail
  • Get the meta
  • Add the meta data for w/h/file
  • Then render the image

    if ( has_post_thumbnail() ) : // check if the post has a Post Thumbnail assigned to it.
    
    $upload_dir = wp_upload_dir();
    
    $size="full";
    
    $post_thumbnail_id = get_post_thumbnail_id( $post_id );
    $post_thumbnail_meta = wp_get_attachment_metadata ( $post_thumbnail_id );
    $main_file = $post_thumbnail_meta [ 'file' ];
    $dirname = dirname ( $main_file );
    $base_url = trailingslashit ( $upload_dir['baseurl'] ) . $dirname . "https://wordpress.stackexchange.com/";
    
    // fallback to known file if the size doesn't exist
    if ( ! isset($post_thumbnail_meta [ 'sizes' ][ $size ] ) )
    {
        $size="full"; // use this size when we're missing data
    }
    
    // full is at the root, alternate sizes exist in the sizes prop
    $imgInfo = $size === 'full' ? $post_thumbnail_meta : $post_thumbnail_meta [ 'sizes' ][ $size ];
    $filename = basename ( $imgInfo[ 'file' ] );
    $width = $imgInfo[ 'width' ];
    $height = $imgInfo[ 'height' ];
    $file = $base_url . $filename;
    
    ?>
    <div itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
        <meta itemprop="url" content="<?php echo $file; ?>">
        <meta itemprop="width" content="<?php echo $width; ?>">
        <meta itemprop="height" content="<?php echo $height; ?>">
    
        <?php
            the_post_thumbnail( 'full', array('class'=>'post_thumbnail_common', 'alt' => get_the_title() , 'title' => get_the_title() ) );
    
            echo contentnoimg(41);
        ?>
    </div>
    <?php
    
    else:  // end has_post_thumbnail else
    
        // no thumbnail
        echo content(41); 
    
    endif; // end has_post_thumbnail block