Featured image with the same height thumbs returns wrong thumb

I have 2 sizes thumbs for featured images with the same height:

  • 300 x 200
  • 540 x 200

I’ve checked the uploads folder and both thumbs with different sizes appear correctly:

Read More
  • file_name-300×200.jpg
  • file_name-540×200.jpg

I’m trying to get the bigger thumb size (540×200) as following:

$width = 540;
$height = 200;
echo get_the_post_thumbnail( $post->ID, array($width,$height) );

However, the image markup that is returned is for the smaller thumb zise (300×200). Interestingly, the class is displayed with the correct domantions as ‘attachment-540×200’:

<img width="300" height="200" src="http://mysite.com/wp-content/uploads/file_name-300x200.jpg" class="attachment-540x200 wp-post-image" alt="file_name" title="file_name" />

Would really appreciate if anyone could explain what’s happening and why the correct size thumb isn’t returned.

Many thanks,
Dasha

Related posts

Leave a Reply

1 comment

  1. I think you’ve hit interesting logic issue in image_get_intermediate_size(). This function loops through available images and tries to find best possible match for custom dimensions you provided.

    First it attempts to find image as per comment in code:

    // already cropped to width or height; so use this size
    

    See the issue? Not image, perfectly matching dimensions, but image that matches in width or height, with remaining dimension being smaller or equal than required to fit in dimensions.

    So your 300×200 image has equal height and fits inside 540×200 dimension you want (and probably comes earlier in meta data) so WP happily grabs it to use.

    Interestingly, the class is displayed with the correct…

    No mystery there, class is simply generated from your input, without regard if values make sense and/or available.