Media: Fix converting of all HEIC/HEIF images to JPEGs after uploading regardless of dimensions.

This backport includes follow up commits to improve a variable name and improve accuracy of when an image needs to be converted.

Reviewed by peterwilsoncc.
Merges [59317], [59346], [59366] to the 6.7 branch.

Props ironprogrammer, adamsilverstein, azaozz, peterwilsoncc, apermo, flixos90.
Fixes #62305.


Built from https://develop.svn.wordpress.org/branches/6.7@59367


git-svn-id: http://core.svn.wordpress.org/branches/6.7@58753 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2024-11-07 01:27:15 +00:00
parent 2612745032
commit 0526401c81
3 changed files with 47 additions and 10 deletions

View File

@ -291,7 +291,26 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
* If the original image's dimensions are over the threshold,
* scale the image and use it as the "full" size.
*/
$scale_down = false;
$convert = false;
if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) {
// The image will be converted if needed on saving.
$scale_down = true;
} else {
// The image may need to be converted regardless of its dimensions.
$output_format = wp_get_image_editor_output_format( $file, $imagesize['mime'] );
if (
is_array( $output_format ) &&
array_key_exists( $imagesize['mime'], $output_format ) &&
$output_format[ $imagesize['mime'] ] !== $imagesize['mime']
) {
$convert = true;
}
}
if ( $scale_down || $convert ) {
$editor = wp_get_image_editor( $file );
if ( is_wp_error( $editor ) ) {
@ -299,14 +318,20 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
return $image_meta;
}
// Resize the image.
if ( $scale_down ) {
// Resize the image. This will also convet it if needed.
$resized = $editor->resize( $threshold, $threshold );
} elseif ( $convert ) {
// The image will be converted (if possible) when saved.
$resized = true;
}
$rotated = null;
// If there is EXIF data, rotate according to EXIF Orientation.
if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
$resized = $editor->maybe_exif_rotate();
$rotated = $resized;
$rotated = $resized; // bool true or WP_Error
}
if ( ! is_wp_error( $resized ) ) {
@ -314,7 +339,11 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
* Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg".
* This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
*/
if ( $scale_down ) {
$saved = $editor->save( $editor->generate_filename( 'scaled' ) );
} else {
$saved = $editor->save();
}
if ( ! is_wp_error( $saved ) ) {
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );

View File

@ -6233,25 +6233,33 @@ function wp_high_priority_element_flag( $value = null ) {
* @return string[] An array of mime type mappings.
*/
function wp_get_image_editor_output_format( $filename, $mime_type ) {
$output_format = array(
'image/heic' => 'image/jpeg',
'image/heif' => 'image/jpeg',
'image/heic-sequence' => 'image/jpeg',
'image/heif-sequence' => 'image/jpeg',
);
/**
* Filters the image editor output format mapping.
*
* Enables filtering the mime type used to save images. By default,
* the mapping array is empty, so the mime type matches the source image.
* Enables filtering the mime type used to save images. By default HEIC/HEIF images
* are converted to JPEGs.
*
* @see WP_Image_Editor::get_output_format()
*
* @since 5.8.0
* @since 6.7.0 The default was changed from array() to array( 'image/heic' => 'image/jpeg' ).
* @since 6.7.0 The default was changed from an empty array to an array
* containing the HEIC/HEIF images mime types.
*
* @param string[] $output_format {
* An array of mime type mappings. Maps a source mime type to a new
* destination mime type. Default maps uploaded HEIC images to JPEG output.
* destination mime type. By default maps HEIC/HEIF input to JPEG output.
*
* @type string ...$0 The new mime type.
* }
* @param string $filename Path to the image.
* @param string $mime_type The source image mime type.
*/
return apply_filters( 'image_editor_output_format', array( 'image/heic' => 'image/jpeg' ), $filename, $mime_type );
return apply_filters( 'image_editor_output_format', $output_format, $filename, $mime_type );
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.7-RC3-59365';
$wp_version = '6.7-RC3-59367';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.