I'm looking for a way to check if an image has properly created a custom thumbnail size that has been hard cropped.

add_image_size( 'homepage-thumb', 1676, 828, true );

$size = "homepage-thumb"; 
echo wp_get_attachment_image( $attachment_id, $size );

This works well if the uploaded image is large enough but if it's smaller than the dimensions defined in add_image_size the aspect will be out which invariably breaks a layout. Is there a way to check the thumbnail before echoing the image?

2 Answers 2

$image_attributes = wp_get_attachment_image_src( $attachment_id, $size ); 

Returns an array containing:

[0] => url
[1] => width
[2] => height
[3] => boolean: true if $url is a re-sized image, false if it is the original. 

Even though you pass the $size as parameter, while returning; it returns the actual width and height of the image irrespective of the image being cropped or not. Then you can check for the width and height if these are smaller then the requirement. OR better apply min-width, min-height in CSS or jQuery.

  • 1
    Yes, that's a good starting point thanks, I'll build it into a function to check against a thumbnail size. CSS is no good as the images need to adjust responsively.
    – Tom
    Dec 9, 2013 at 11:38

I guess you can do that using CSS, use Inspect Element facility of browser to confirm. Let me know if you are looking for something else. This is not an answer, but I have low reputation to comment on your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.