phpwordpress

Get height and width from image in wordpress


I'm trying to make an info block on my WordPress website I am working on. I want to get the height X width of the photo:

get_the_post_thumbnail_url() 

I want it printed out on the page something like:

image size: 1234x123 1.2mb

I just don't know how to make it. I have tried a lot of different examples I found on the web but I always get different errors.

Hope someone has an idea of a good way to do this. :)


Solution

  • This WP function wp_get_attachment_image_src, provides image width and height:

    Read here more: https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/

    From docs, it says that it Returns:

    Returns an array (url, width, height, is_intermediate), or false, if no img.

    For Example:

      $img = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), "full" );
    

    Now

    $url = $img[0];
    $width = $img[1];
    $height = $img[2];