10

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?

2
  • 1
    Are you talking about registered thumbnail sizes or you are trying to get arbitrary non-registered size in pixels? Code example please?
    – Rarst
    Commented Jan 27, 2011 at 8:31
  • about the registered sizes
    – Alex
    Commented Jan 29, 2011 at 9:30

7 Answers 7

2

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.

1

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.

1

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.

0

There's a plugin that will actually create the image size automatically for you if it doesn't exist. http://austinmatzko.com/wordpress-plugins/filosofo-custom-image-sizes/

I haven't needed to use this plugin yet myself. But it's worth a shot. There is talk about adding similar functionality to WP core, maybe in 3.2 http://core.trac.wordpress.org/ticket/15311

0

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 
  }
0
0

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] ) . '" />';
}
1
  • You assume that, just because a size is defined in the theme or via a plugin, it also has a generated thumbnail, which is a wrong assumption.
    – brett
    Commented Mar 28 at 21:42
-1

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.

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.