Skip to content

Commit b781748

Browse files
committed
Media: Fix converting of all HEIC/HEIF images to JPEGs after uploading regardless of dimensions.
Props ironprogrammer, adamsilverstein, azaozz. Fixes #62305. git-svn-id: https://develop.svn.wordpress.org/trunk@59317 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1913680 commit b781748

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

src/wp-admin/includes/image.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,30 +291,55 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
291291
* If the original image's dimensions are over the threshold,
292292
* scale the image and use it as the "full" size.
293293
*/
294+
$scale_down = false;
295+
$convert = false;
296+
294297
if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) {
298+
// The image will be converted if needed on saving.
299+
$scale_down = true;
300+
} else {
301+
// The image may need to be converted regardless of its dimensions.
302+
$output_format = wp_get_image_editor_output_format( $file, $imagesize['mime'] );
303+
304+
if ( is_array( $output_format ) && array_key_exists( $imagesize['mime'], $output_format ) ) {
305+
$convert = true;
306+
}
307+
}
308+
309+
if ( $scale_down || $convert ) {
295310
$editor = wp_get_image_editor( $file );
296311

297312
if ( is_wp_error( $editor ) ) {
298313
// This image cannot be edited.
299314
return $image_meta;
300315
}
301316

302-
// Resize the image.
303-
$resized = $editor->resize( $threshold, $threshold );
317+
if ( $scale_down ) {
318+
// Resize the image. This will also convet it if needed.
319+
$resized = $editor->resize( $threshold, $threshold );
320+
} elseif ( $convert ) {
321+
// The image will be converted (if possible) when saved.
322+
$resized = true;
323+
}
324+
304325
$rotated = null;
305326

306327
// If there is EXIF data, rotate according to EXIF Orientation.
307328
if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
308329
$resized = $editor->maybe_exif_rotate();
309-
$rotated = $resized;
330+
$rotated = $resized; // bool true or WP_Error
310331
}
311332

312333
if ( ! is_wp_error( $resized ) ) {
313334
/*
314335
* Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg".
315336
* This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
316337
*/
317-
$saved = $editor->save( $editor->generate_filename( 'scaled' ) );
338+
if ( $scale_down ) {
339+
$saved = $editor->save( $editor->generate_filename( 'scaled' ) );
340+
} else {
341+
$saved = $editor->save();
342+
}
318343

319344
if ( ! is_wp_error( $saved ) ) {
320345
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );

src/wp-includes/media.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6233,16 +6233,23 @@ function wp_high_priority_element_flag( $value = null ) {
62336233
* @return string[] An array of mime type mappings.
62346234
*/
62356235
function wp_get_image_editor_output_format( $filename, $mime_type ) {
6236+
$default_output_format = array(
6237+
'image/heic' => 'image/jpeg',
6238+
'image/heif' => 'image/jpeg',
6239+
'image/heic-sequence' => 'image/jpeg',
6240+
'image/heif-sequence' => 'image/jpeg',
6241+
);
6242+
62366243
/**
62376244
* Filters the image editor output format mapping.
62386245
*
6239-
* Enables filtering the mime type used to save images. By default,
6240-
* the mapping array is empty, so the mime type matches the source image.
6246+
* Enables filtering the mime type used to save images. By default HEIC/HEIF images
6247+
* are converted to JPEGs.
62416248
*
62426249
* @see WP_Image_Editor::get_output_format()
62436250
*
62446251
* @since 5.8.0
6245-
* @since 6.7.0 The default was changed from array() to array( 'image/heic' => 'image/jpeg' ).
6252+
* @since 6.7.0 The default was changed from empty array to array containing the HEIC mime types.
62466253
*
62476254
* @param string[] $output_format {
62486255
* 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 ) {
62536260
* @param string $filename Path to the image.
62546261
* @param string $mime_type The source image mime type.
62556262
*/
6256-
return apply_filters( 'image_editor_output_format', array( 'image/heic' => 'image/jpeg' ), $filename, $mime_type );
6263+
return apply_filters( 'image_editor_output_format', $default_output_format, $filename, $mime_type );
62576264
}

0 commit comments

Comments
 (0)