Skip to content

Commit

Permalink
Media: Fix converting of all HEIC/HEIF images to JPEGs after uploadin…
Browse files Browse the repository at this point in the history
…g 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.



git-svn-id: https://develop.svn.wordpress.org/branches/6.7@59367 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Nov 7, 2024
1 parent 630da95 commit 50fc902
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
37 changes: 33 additions & 4 deletions src/wp-admin/includes/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,30 +291,59 @@ 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 ) ) {
// This image cannot be edited.
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 ) ) {
/*
* 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 );
Expand Down
18 changes: 13 additions & 5 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

0 comments on commit 50fc902

Please sign in to comment.