Home » WordPress Tutorials » You are reading »

How to Check if Custom Image Size Exists in WordPress

When working on a custom WP Theme last night, I’m looking into how to check if custom image size exists in WordPress posts or pages. Since WordPress version 2.9, the core introduce and add support for ‘auto image size’ for each WordPress posts. Without relying on third party thumbnail re-sizer such as Timthumb, user now can upload images into the write post panel and the WordPress core will auto create your available add_image_size() set image size. learn more about add_image_size() here.

check-if-custom-image-size-exist

You can grabbed your existing ‘image size’ by using the_post_thumbnail() anywhere in your template, provide if the ‘image size’ existed in the first place. WordPress core already include default ‘image size’ when you uploaded any images into your media or attachment settings. This include ‘image size’ for thumbnail, medium, large and full. The following image size can be showed with below code.

the_post_thumbnail('thumbnail'); // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium');// Medium resolution (default 300px x 300px max)
the_post_thumbnail('large');// Large resolution (default 640px x 640px max)
the_post_thumbnail('full'); // Full resolution (original size uploaded)

But what if you have a different add_image_size() that you want to use instead of the default one. You want to grabbed your own custom image size first before WordPress core kick in and use the closest available image size by default. Here’s how you can accomplished this:

First open your functions.php and check for your custom image size, it will look something like this

add_image_size( 'my-thumbnail', 100, 100, true ); //hard crop 100x100

And now in your template post loop, you can grabbed your ‘custom image size’ first before resolving to default ‘thumbnail’ by adding similar code below.

<?php
$the_thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "my-thumbnail" );
$thumbnail_src = $the_thumbnail_src[0];
//change this to exact WxH of your custom image size
$check_thumb = strpos($thumbnail_src, '100x100'); 

// if custom image size exist
if( $check_thumb ):
echo the_post_thumbnail('my-thumbnail');
else:
echo the_post_thumbnail('thumbnail'); // if not, use default thumbnail
endif;

?>

That’s all, its easy right? Now you can check if your own set of custom image size exist or not before WordPress core use the default or closest ‘image size’.

If you want to regenerate your own set of custom image size, you can try Ajax Thumbnail Rebuild which can help you re-build your featured images and existing or new ‘image size’.

Related Topics:

WordPress Tutorials
Building Your Website with WordPress? Learn How to Optimize it
WordPress Tutorials
How to Detect Mobile or Tablet Condition in WordPress
WordPress Tutorials
Designing and Protecting Your WordPress Site is Easier Than You Think
WordPress Tutorials
The Ultimate SEO Tips For Your WordPress Blog
How to Check if Custom Image Size Exists in WordPresshttp://cdn.dezzain.com/1/2013/06/check-if-custom-image-size-exist.jpghttp://cdn.dezzain.com/1/2013/06/check-if-custom-image-size-exist-150x150.jpgRichie KS WordPress Tutorialshow to,image size,wordpress codingLearn how to check if custom image size exist in WordPress. You can use this tutorial and snippet to check if your own set of custom image size existed in your WordPress media uploads. DEZZAIN.COM
Enjoy This Article?, Follow us or Share it smiley
viral and buzz WordPress theme

5 Comments Already

  1. Great post. One alternative suggestion would be to use the width and height returned by wp_get_attachment_image_src(). Along with the SRC value as the first [0] item in the returned array, the image width and height are stored in [1] and [2] respectively, so you could do a simple comparison, like:

    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "my-thumbnail" );
    if( $image[1] <= '900' ): // Checks if width is <= 900px
      echo '';
    else:
    // ... do something else
    endif;
    

    This would be a better way of handling the use cases mentioned in the comments where a height or width might be unknown, but where for display purposes you want to make sure you have an image of a specific size. Of course, you could set the “>=” to “==” if you need to make a direct comparison. I suspect this would also have slightly better performance, as you’re not running a function (strpos) on the value.

    Public CommentUserGreat post. One alternative suggestion would be to use the width and height returned by wp_get_attachment_image_src(). Along with the SRC value as the first [0] item in the returned array, the image width and height are stored in [1] and [2] respectively, so you could do a simple comparison, like: This would be a better way of handling the use cases mentioned in the comments where a height or width might be unknown, but where for display purposes you want to make sure you have an image of a specific size. Of course, you could set the ">=" to "==" if you need to make a direct comparison. I suspect this would also have slightly better performance, as you're not running a function (strpos) on the value.
    • thx for the input Ryan, yes this will do too…

      Member of DEZZAIN.COMAdministratorthx for the input Ryan, yes this will do too...
  2. The desktop view is superB but my site images not displaying when anybody is opens it in mobile and some text is also overlapping. So, i am unable to use the responsive feature of this theme.
    http://www.mefashionagency.in

    Public CommentUserThe desktop view is superB but my site images not displaying when anybody is opens it in mobile and some text is also overlapping. So, i am unable to use the responsive feature of this theme. www.mefashionagency.in
  3. This is too specific to be useful in any case where you are allowing a flexible height or width and are cropping the image through WordPress.

    When doing that, you can’t check the height and width of the image against the static definition, so this check then fails.

    Public CommentUserThis is too specific to be useful in any case where you are allowing a flexible height or width and are cropping the image through Wordpress. When doing that, you can't check the height and width of the image against the static definition, so this check then fails.
    • yeap but with the hard cropped method, newly uploaded image with set_image() 300×300 hard-crop true will get cropped to 300×300 exact so it may not passed for previous uploaded featured image per-say.

      Member of DEZZAIN.COMAdministratoryeap but with the hard cropped method, newly uploaded image with set_image() 300x300 hard-crop true will get cropped to 300x300 exact so it may not passed for previous uploaded featured image per-say.

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


You can wrap php, js or style css code with [PHP]..your code...[/PHP]