How to trigger “wp_handle_upload_prefilter” filter when uploading an image programatically?

I eventually got this. Solution was to use wp_handle_upload() function. Kinda obvious, when I come to think about it…

function handle_upload( string $path ): array {
    $filename        = basename( $path );
    $random_filename = rand( 0, 1000 ) . $filename;
    $tmp_path        = str_replace( $filename, $random_filename, $path );

    /** Create a temporary copy of the original file, since it will be moved during the upload process */
    copy( $path, $tmp_path );

    if ( file_exists( $tmp_path ) ) {
        $_files_mimic = [
            'name'     => $random_filename,
            'type'     => 'image/svg+xml',
            'tmp_name' => $tmp_path,
            'error'    => 0,
            'size'     => filesize( $tmp_path ),
        ];

        return wp_handle_upload( $_files_mimic, [
            'action'    => 'test_svg_upload',
            'test_form' => false,
        ] );
    } else {
        throw new RuntimeException( 'Could not copy test file from ' . $path . ' to ' . $tmp_path );
    }
}

Usage:

$upload_response = handle_upload('path/to/my/image.jpg');

Now if you debug $upload_response['file'], you should have your media processed by wp_handle_upload_filter, so you can test it or something.