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

Props ironprogrammer, adamsilverstein, azaozz.
Fixes #62305.
Built from https://develop.svn.wordpress.org/trunk@59317


git-svn-id: http://core.svn.wordpress.org/trunk@58703 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2024-10-29 06:01:19 +00:00
parent 35264ca89c
commit c53b87650b
3 changed files with 41 additions and 9 deletions

View File

@ -291,7 +291,22 @@ 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 ) ) {
$convert = true;
}
}
if ( $scale_down || $convert ) {
$editor = wp_get_image_editor( $file );
if ( is_wp_error( $editor ) ) {
@ -299,14 +314,20 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
return $image_meta;
}
// Resize the image.
$resized = $editor->resize( $threshold, $threshold );
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 +335,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).
*/
$saved = $editor->save( $editor->generate_filename( 'scaled' ) );
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,16 +6233,23 @@ 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 ) {
$default_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 empty array to array containing the HEIC mime types.
*
* @param string[] $output_format {
* An array of mime type mappings. Maps a source mime type to a new
@ -6253,5 +6260,5 @@ function wp_get_image_editor_output_format( $filename, $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', $default_output_format, $filename, $mime_type );
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.8-alpha-59316';
$wp_version = '6.8-alpha-59317';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.