You may occasionally want to regenerate image thumbnails in WordPress (such as after changing image quality settings). Here is a handy function that regenerates all thumbnails for a given attachment post
function regenerate_attachment_images( $attachment_id )
{
// We only want to look at image attachments
if ( !wp_attachment_is_image($attachment_id) )
return;
$filepath = get_attached_file( $attachment_id, true );
$metadata = wp_generate_attachment_metadata( $attachment_id, $filepath );
// Was there an error?
if ( is_wp_error( $metadata ) )
$this->die_json_error_msg( $attachment_id, $metadata->get_error_message() );
if ( empty( $metadata ) )
$this->die_json_error_msg( $attachment_id, 'Unknown failure reason.' );
// If this fails, then it just means that nothing was changed (old value == new value)
wp_update_attachment_metadata( $attachment_id, $metadata );
}
The above was taken mostly from the Regenerate Thumbnails plugin with a few modifications of my own.
Example Usage
To regenerate all thumbnails for all images use the following (Warning: This could take a while if you have alot of images and/or image sizes):
$images = new WP_Query(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_mime_type' => 'image',
'suppress_filters' => false,
'offset' => 0,
'post_status' => 'inherit',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
));
while ( $images->have_posts() ):
$images->next_post();
regenerate_attachment_images( $images->post->ID );
endwhile;



