[wp-hackers] add_image_size() possible to restrict to just one size?..

Mike Schinkel mikeschinkel at newclarity.net
Sun Sep 5 21:39:32 UTC 2010


Hi Patrik,

That was really interesting, thanks for sharing.  I think I need this for something I'm working on today! 

I have one question though, on this line:

> 	$metadata['sizes']['thumbnail'] = array('file' =>  basename($file), 'width' =>  100, 'height' =>  100);


Are you replacing the thumbnail with your custom size?  I'm unclear on what you are attempting to achieve here (maybe because I'm not 100% clear how WordPress' image handling all works, but not for lack of inspecting the code that does image handling.)

-Mike

On Sep 5, 2010, at 6:44 AM, Patrik Bóna wrote:

> Hi Davit,
> 
> You can use wp_generate_attachment_metadata filter. I use it for generating own image sizes for different post types. This is example for post type galeria (gallery):
> 
> function gallery_upload($metadata, $attachment_id) {
> 	$post_id = $_POST['post_id'];
> 
> 	// if it is not 'galeria' just return metadata
> 	if(get_post_type($post_id) != 'galeria')
> 		return $metadata;
> 
> 	// otherwise create custom image sizes
> 	$upload_dir = wp_upload_dir();
> 	$file = image_resize($upload_dir['basedir'] . '/' . $metadata['file'], 100, 100, true, 'thumb');
> 	$metadata['sizes']['thumbnail'] = array('file' =>  basename($file), 'width' =>  100, 'height' =>  100);
> 	return $metadata;
> }
> add_filter('wp_generate_attachment_metadata', 'gallery_upload', 10, 2);
> 
> So i don't use add_image_size (in this case) because it apply to everything. Disadvantage of this solution is that these sizes are not deleted automatically when you delete attachment, but you can again use filters/actions... ;)
> 
> Patrik
> 
> On 09/05/2010 12:17 PM, Davit Barbakadze wrote:
>> In my understanding now, whenever I add new size, wordpress just
>> automatically creates it on every upload. That is if I got five
>> different size, for post thumbnails, for sidebar widgets, avatars,
>> custom category headers, etc - they all get created on every upload.
>> Is it so? If for example if I upload an image for an avatar, do the
>> post thumbnail size, and sidebar widget size and category header size
>> get created too? If yes, can I prevent it, and restrict maybe to only
>> an avatar size for that particular case?
>> 
>>   
> 
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list