WordPress.org

Ready to get started?Download WordPress

Forums

How to get size of thumbnail? (8 posts)

  1. iiShrimp
    Member
    Posted 3 years ago #

    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.

  2. iiShrimp
    Member
    Posted 2 years ago #

    SOmebody?

  3. Kraignos
    Member
    Posted 2 years ago #

  4. webmystery
    Member
    Posted 2 years ago #

    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.

  5. SpankMarvin
    Member
    Posted 2 years ago #

    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];
  6. Digital Raindrops
    Member
    Posted 2 years ago #

    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!

    HTH

    David

  7. webmystery
    Member
    Posted 2 years ago #

    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){
    $isize=(getimagesize($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.

  8. Digital Raindrops
    Member
    Posted 2 years ago #

    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 );

    Regards

    David

Topic Closed

This topic has been closed to new replies.

About this Topic