Support » Themes and Templates » How to get size of thumbnail?

  • With the use of the function

    the_post_thumbnail( 'thumbnail' );

    I am able to get the post thumbnail in size which was set in the administration panel. What I need is to get the dimensions of the thumbnail (width and height) separetely, since there is more than one possible thumbnail size defined and I need to know the exact size of thumbnail for the post.

Viewing 7 replies - 1 through 7 (of 7 total)
  • SOmebody?

    I am trying to get the width of attached images on a loop after they’ve been resized as thumbnails with a fixed height and the image ratio preserved.

    I am able to get the width of the original post thumbnail, but having trouble finding the resized width.

    Any tips appreciated.

    Use wp_get_attachment_image_src() function, with the id as the first parameter, and the size (e.g. ‘thumbnail’) as the second parameter, e.g.:

    $tn_id = get_post_thumbnail_id( $post->ID );
    $img = wp_get_attachment_image_src( $tn_id, 'thumbnail' );
    $width = $img[1];
    $height = $img[2];

    since there is more than one possible thumbnail size

    Not sure what you mean by that, more than one?

    Or why you would need the height?

    When the thumbnail is created it will always size to the width, so your height style can be set as height: auto; ?

    Have a look at the Codex get_intermediate_image_sizes()

    This will return an array of all image sizes!



    Thanks, SpankMarvin – that worked!

    Digital Raindrops- If your thumbnail setting preserves image ratio to a fixed height, eg: height=200 and width=9999, you won’t know the width.

    My dear graphic designer friend, who knows just enough about WordPress to be dangerous, gave me a spec for an artist gallery web site “the short side of images should be 100px, preserving the image ratio, with 20px padding between the thumbnails…”. From a design point of view she’s correct. If you use only one thumbnail setting than either your landcape or portrait images will be resized smaller than the others. The she wanted them bottom aligned in the rows rather then the default top alignment. I defined a few different thumbnail sizes, then wrote a function to find whether they are portrait or landscape:

    `function is_portrait($image_url){
    //array 0 is height, 1 is width
    if ($isize[0] >=$isize[1]){return true; }
    else{return false;}

    I modified the gallery shortcode to set the size according to portait or landscape size.

    I was able to bottom align the thumbs using absoute positioning in the child element, but without specifying a width they all landed on top of each other. Setting the width of the container at the element level fixed this. The site isn’t live yet or I’d post a link.

    Hi Webmystry,
    I just had a look at the WordPress default ‘thumbnail’ creation 150px * 150px, and WordPress crops the longest side in ratio to the other, eg: portrait crops top and bottom, landscape left and right.

    As the default has no option and will ‘hard crops’ the thumbnail it is always going to be 150px * 150px, or whatever is set?

    That was where my confusion was, as it looks like yours is a custom_image_size, not the default ‘thumbnail’ function, and not the standard WordPress thumbnail set in the Admin > Media settings.

    add_image_size( 'my-thumbnail', $width, $height, $crop );



Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to get size of thumbnail?’ is closed to new replies.