Rename image file by post title

I think what you’re looking for is the filter: wp_handle_upload_prefilter.

From the Codex:

The single parameter, $file, represent a single element of the $_FILES
array. The wp_handle_upload_prefilter provides you with an opportunity
to examine or alter the filename before the file is moved to its final
location.

Example code from the Codex:

add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );

function custom_upload_filter( $file ){
    $file['name'] = 'wordpress-is-awesome-' . $file['name'];
    return $file;
}

How to use this hook

add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ) {
    if ( ! isset( $_REQUEST['post_id'] ) ) {
        return $file;
    }
    $id           = intval( $_REQUEST['post_id'] );
    $parent_post  = get_post( $id );
    $post_name    = sanitize_title( $parent_post->post_title );
    $file['name'] = $post_name . '-' . $file['name'];
    return $file;
}

error code: 523