Display thumbnail only if requested size exists

When I use the functions that get and output the post thumbnail, they will display the requested thumbnail in the size you specify, but if this size doesn’t exist they will display the thumbnail in the original size and force the browser to resize it.

How can I force them to display a placeholder like “no image” in case the requested size doesn’t exist?

Related posts

Leave a Reply

7 comments

  1. Assuming the question is about wp_get_attachment_image_src.
    It could be also about wp_get_attachment_link, but, although related, this analysis doesn’t includes it.


    Got aware of this issue answering this question: How can I view all WP generated thumbnails in Media Manager?.
    Refer to it to see a working code regarding the issue on this Question.

    And it lead to this WordPress forum topic.


    The function wp_get_attachment_image_src( $attachment_id, $size, $icon ) returns an array containing:

    [0] => url  
    [1] => width  
    [2] => height  
    [3] => is intermediate
    

    If [3] is false, the original image data is returned.


    Both wp_get_attachment_image_src and wp_get_attachment_link rely in the function image_downsize, inside /wp-includes/media.php.
    And that’s where this 4 items array is being returned.

  2. I’m Not sure about this but i know you can use php’s getimagesize() function
    something like this:

    //This must be in one loop
    
    if(has_post_thumbnail()) {
        $wanted_width = 300;
        $wanted_height = 150;
        $id = get_post_thumbnail_id()
        $image = wp_get_attachment_image_src($id);
        if ($image){
            list($width, $height) = getimagesize($image[0]);
            if (($wanted_height = $width) && ($wanted_height = $height)){
                the_post_thumbnail();
            }
            else{
                //default when no image
                echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
            }
        }
        else{
            //default when no image
            echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
        }
    } else {
        //default when no image
        echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
    }
    

    and i know it’s not an answer to the question directly but you can use a plugin called AJAX Thumbnail Rebuild
    This plugin allows you to re-build the post thumbnails. Useful if you add_image_size() after already having uploaded post thumbnails.

  3. This is how I was able to display a thumbnail only if a requested size exists:

    if ( has_post_thumbnail() )  {
        $imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail' ); //change thumbnail to whatever size you are using
        $imgwidth = $imgdata[1]; // thumbnail's width                   
        $wanted_width = 300; //change this to your liking
        if ( ($imgwidth < $wanted_width ) ) {
            the_post_thumbnail();
        } else { 
            //do whatever you want
        }
    }
    

    To learn more about wp_get_attachment_image_src read the Codex.

  4. It’s seem’s to be a better way.
    Use the global variable $_wp_additional_image_sizes, it’s store all registered image_size.
    So if you want to check if an image size name is define :

      global $_wp_additional_image_sizes;
      if ( array_key_exists( 'desired-image-size' , $_wp_additional_image_sizes) )
      {
        //your code here
      }
      else
      {
        //default code here 
      }
    
  5. The best approach is to use WordPress’ built-in “get_intermediate_image_sizes” function to return the full list of image sizes as an array then look through that.

    show_image_by_size( 10470, 'custom_size' );
    
    function show_image_by_size( $_post_id, $_desired_size ) {
      $_sizes = get_intermediate_image_sizes();
      $_thumbnail_id = get_post_thumbnail_id( $_post_id );
      $_image_size = ( in_array( $_desired_size , $_sizes ) ) ? $_desired_size : 'full';
      $image = wp_get_attachment_image_src( $_thumbnail_id, $_image_size );
      echo '<img src="' . esc_url( $image[0] ) . '" />';
    }
    
  6. Maybe this will help

    <?php 
    //This must be in a loop
    if(has_post_thumbnail()) {
        the_post_thumbnail();
    } else {
        echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
    }
    ?>
    

    from: http://codex.wordpress.org/Function_Reference/has_post_thumbnail
    I use something like this to check if the image size exists:

    if (function_exists('has_post_thumbnail') && has_post_thumbnail('medium')) {
    //// code to display thumbnail goes here
    }
    else {
    /// otherwise do this
    }
    

    Hope it helps get you going.