Regenerate thumbnails after upload

I have this nice little code coming straight from the WordPress codex

if($_FILES['user_picture']['size'] != 0):
            if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
            $uploadedfile = $_FILES['user_picture'];
            $upload_overrides = array( 'test_form' => false );
            $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
            if ( $movefile ) {
                $wp_filetype = $movefile['type'];
                $filename = $movefile['file'];
                $wp_upload_dir = wp_upload_dir();
                $attachment = array(
                    'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
                    'post_mime_type' => $wp_filetype,
                    'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)),
                    'post_content' => '',
                    'post_status' => 'inherit'
                );
                $attach_id = wp_insert_attachment( $attachment, $filename);
            }
        endif;

The thing is that it works great but it doesn’t create all the image size set in the function.php. I don’t want to run a plugin that regenerate a thumbnail each time I upload an image, there must be a way to create all those thumbs after upload. How ?

Read More

Update

After some investigation on how other script work, I need to get the file back and use the wp_get_image_editor function to make my thumbs… kinda a pain in the know what. Can’t believe that wordpress don’t resize images after running wp_handle_upload or wp_insert_attachment.

Related posts

1 comment

  1. Adding

    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id,  $attach_data );
    

    after

    $attach_id = wp_insert_attachment( $attachment, $filename);
    

    Fixed it.

Comments are closed.