I would like to do some custom processing on a certain thumbnail size in WordPress with ImageMagick beyond the normal WordPress functionality and am not quite sure how to go about doing this.
So I add my new thumbnail size:
add_image_size( 'new-thumb', 100, 100 );
And then this is where I’m not sure where I should hook into. Before the final copy of the thumbnail is saved within WordPress I want to do some custom processing on it. Basic psuedo code for what I want is:
The_hook_or_action_that_fires_when_a_thumbnail_is_saved() {
if (<Thumbnail Being Generated> == 'new-thumb') {
$thumb_file = 'the thumbnail image we are about to save';
$thumbfile = 'do some imagemagic stuff here';
}
save_thumbnail;
}
I can handle the imagemagick stuff, but I’m not sure how / where to hook this custom thumbnail processing into.
Any advise would be greatly appreciated!
Thanks @brasofilo for pointing me in the right direction…
I poked around a bit and figured this one out. You can hook into wp_generate_attachment_metadata and do some image manipulation.
The basics of what I was trying to do is resize an image to a specific size (“brands” thumbnail), then expand the canvas for that thumbnail to a static height and width with a white background.
In case anyone has a similar situation I thought I would paste some code. It could be cleaned up to remove the repletion for each image type. The one issue with this code is that if the original image size is less than the desired thumbnail size it will not be generated (which is WordPress functionality).
In my library I have the following:
Set custom name for generated thumbnails
Very interesting manipulation of thumb names and crop. Check the original Q&A linked in this one.
Uses:
intermediate_image_sizes_advanced
wp_generate_attachment_metadata
Automatically Use Resized Images Instead Of Originals
Uses:
wp_generate_attachment_metadata
How to automatically add rounded corners to thumbnails?
Uses:
image_make_intermediate_size
and L#432How to Require a Minimum Image Dimension for Uploading?
Uses:
wp_handle_upload_prefilter
Organize uploads by year, month and day
Uses:
wp_handle_upload_prefilter
wp_handle_upload
and L#466upload_dir
I would like to suggesta change, in order to make it work with Thumbnails Regenerate Plugin
That’s so because wp_upload_dir returns the path for the current year-month but some previously uploaded images could have different path.