From 620b66b8672078b44b45c90c1ff42fe2c5bd7b46 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Sun, 15 Mar 2026 14:17:18 -0700 Subject: [PATCH] Media: Update post mime type when image format is converted during upload. When uploading HEIC files via the media library on servers that support HEIC, the image is correctly converted to JPEG on disk but the attachment post's `post_mime_type` remained `image/heic`. This updates `_wp_image_meta_replace_original()` to also update the post mime type when the saved image format differs from the original. Additionally fixes the REST API index to use `wp_get_image_editor_output_format()` so the `image_output_formats` response includes the default HEIC-to-JPEG mapping, enabling client-side media processing to be aware of it. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/wp-admin/includes/image.php | 14 ++++++++++++++ src/wp-includes/rest-api/class-wp-rest-server.php | 6 ++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index 95084b1db0576..e1bfb8e67558b 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -209,6 +209,20 @@ function _wp_image_meta_replace_original( $saved_data, $original_file, $image_me // Update the attached file meta. update_attached_file( $attachment_id, $new_file ); + // Update the post mime type if the image was converted to a different format. + if ( ! empty( $saved_data['mime-type'] ) ) { + $post = get_post( $attachment_id ); + + if ( $post && $post->post_mime_type !== $saved_data['mime-type'] ) { + wp_update_post( + array( + 'ID' => $attachment_id, + 'post_mime_type' => $saved_data['mime-type'], + ) + ); + } + } + // Width and height of the new image. $image_meta['width'] = $saved_data['width']; $image_meta['height'] = $saved_data['height']; diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index 704a990298826..a95de2a8d5b8f 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -1383,8 +1383,10 @@ public function get_index( $request ) { $input_formats = array( 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/heic' ); $output_formats = array(); foreach ( $input_formats as $mime_type ) { - /** This filter is documented in wp-includes/media.php */ - $output_formats = apply_filters( 'image_editor_output_format', $output_formats, '', $mime_type ); + $output_format = wp_get_image_editor_output_format( '', $mime_type ); + if ( ! empty( $output_format[ $mime_type ] ) && $output_format[ $mime_type ] !== $mime_type ) { + $output_formats[ $mime_type ] = $output_format[ $mime_type ]; + } } $available['image_output_formats'] = (object) $output_formats;