diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php
index 773be72833..d1700d9fa0 100644
--- a/src/wp-admin/includes/ajax-actions.php
+++ b/src/wp-admin/includes/ajax-actions.php
@@ -2441,6 +2441,12 @@ function wp_ajax_media_create_image_subsizes() {
 		}
 	}
 
+	// Set a custom header with the attachment_id.
+	// Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
+	if ( ! headers_sent() ) {
+		header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
+	}
+
 	// This can still be pretty slow and cause timeout or out of memory errors.
 	// The js that handles the response would need to also handle HTTP 500 errors.
 	wp_update_image_subsizes( $attachment_id );
diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php
index 1582ceb3df..31577acc25 100644
--- a/src/wp-admin/includes/image.php
+++ b/src/wp-admin/includes/image.php
@@ -251,12 +251,6 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
 			return $image_meta;
 		}
 
-		// Set a custom header with the attachment_id.
-		// Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
-		if ( ! headers_sent() ) {
-			header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
-		}
-
 		// Resize the image
 		$resized = $editor->resize( $threshold, $threshold );
 		$rotated = null;
@@ -297,10 +291,6 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
 			return $image_meta;
 		}
 
-		if ( ! headers_sent() ) {
-			header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
-		}
-
 		// Rotate the image
 		$rotated = $editor->maybe_exif_rotate();
 
@@ -400,12 +390,6 @@ function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
 		return $image_meta;
 	}
 
-	// Set a custom header with the attachment_id.
-	// Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
-	if ( ! headers_sent() ) {
-		header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
-	}
-
 	// If stored EXIF data exists, rotate the source image before creating sub-sizes.
 	if ( ! empty( $image_meta['image_meta'] ) ) {
 		$rotated = $editor->maybe_exif_rotate();
diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php
index 18e87baa71..b9e9acc99d 100644
--- a/src/wp-admin/includes/media.php
+++ b/src/wp-admin/includes/media.php
@@ -408,6 +408,12 @@ function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrid
 	$attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true );
 
 	if ( ! is_wp_error( $attachment_id ) ) {
+		// Set a custom header with the attachment_id.
+		// Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
+		if ( ! headers_sent() ) {
+			header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
+		}
+
 		// The image sub-sizes are created during wp_generate_attachment_metadata().
 		// This is generally slow and may cause timeouts or out of memory errors.
 		wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
