Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
set_post_thumbnail_size( 80, 80, true ); // true for all post types

Suppose I have two post types in play on my blog: Post, Video and Product. I may want a 80x80 image size for my posts, but 100x100 for my videos and a 50x100 for my Products. To my knowledge, I am forced to create thumbnails for my Post images in the dimensions of 100x100 and also 50x100. And for my products, I'm forced to create thumbnails that are 80x80 and 100x100, even though I won't use those ever.

Is there a way to create image sizes that are based on post types so you don't end up making a bunch of images you don't plan on using, and wasting resources that could be used elsewhere?

share|improve this question
    
I've run into exactly the same need. –  MikeSchinkel Nov 26 '10 at 22:34
add comment

4 Answers 4

I came up with following solution. It does create images only for custom post type without creating any additional ones. The only problem is that if you use plugins like advanced custom fields, when trying to render the gallery field it only shows the default wordpress images sizes: 'thumbnail', 'medium', 'large' and you cannot access custom post image sizes created.

if ( function_exists ('add_image_size') ) {

function remove_default_img($sizes) {
    unset( $sizes['medium']);
    unset( $sizes['large']); 
    return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'remove_default_img');

$type = get_post_type($_REQUEST['post_id']);
// Post_Name Image Sizes ======================================================
if ( $type === "Custom_Post_Name" || $post_type === "Custom_Post_Name" ) {
add_image_size('custom_size_img', 165, 249, true);
}

}

share|improve this answer
add comment

What happens if you use this function image_resize in the theme in the parts where you need that image size ?, you can also write a shorthand function for multiple uses depending on the context.

share|improve this answer
add comment

I posted some code on this other thread which sets a transient with the post type when uploading an image, and then checks for the value of that transient and adjusts the post-thumbnail size accordingly when the upload is finished. Its not perfect or bulletproof, but it should demonstrate the concept at least.

share|improve this answer
add comment

There is no way to base the image sizes on the post type, as images are not yet attached to a post when you upload them and the intermediate image sizes are created (when you just add them via "New Media" they are not even attached to any post at all).

However, I recently created two plugins that together will do what you want. Virtual intermediate images will intercept the creation of the intermediate image sizes: they will not be created on disk, but still exist in the metadata array. This makes WordPress think they exist so you can use them like normal images. The companion plugin, On-Demand Resizer will create missing image sizes when they are requested. The first time the image is requested it is created, all subsequent requests will just be served the newly created image.

These plugins need a rewrite before I put them in the WordPress plugin directory, but they should be safe. Or at least have TODO comments where they are unsafe.

share|improve this answer
    
Nice! –  MikeSchinkel Nov 26 '10 at 22:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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