Recently, I was working with a WordPress theme that includes a “featured image” (added recently to WordPress). Instead of just displaying it on the frontend, I wanted to know how big it was so that I could add a css class to an element – changing the layout – depending if someone had uploaded a “small” or a “large” featured image.

You can enable featured images in your theme by adding the following to your functions.php file.

This will add support to both posts and pages. If you just want to do one or the other, add either of the following instead:

To show this image on the frontend without modification, you’d use one of the following:

This would add the image tag, alt text etc to the page whereever you call it.

As I explained above though, I wanted to get the dimensions of the image to affect the layout. As this method displays the full image tag, what we need to do is grab the URL to the image, and check its dimensions. We can achieve this by using the function wp_get_attachment_image_src and grabbing the width and height using the PHP function getimagesize

Image Credit: gnuckx