Make WordPress Core


Ignore:
Timestamp:
10/29/2024 05:59:10 AM (16 months ago)
Author:
azaozz
Message:

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

Props ironprogrammer, adamsilverstein, azaozz.
Fixes #62305.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image.php

    r58849 r59317  
    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
     
    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
     
    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
     
    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 ) ) {
Note: See TracChangeset for help on using the changeset viewer.