Make WordPress Core


Ignore:
Timestamp:
11/07/2024 01:25:10 AM (15 months ago)
Author:
peterwilsoncc
Message:

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.

Location:
branches/6.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.7

  • branches/6.7/src/wp-admin/includes/image.php

    r58849 r59367  
    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 (
     305                is_array( $output_format ) &&
     306                array_key_exists( $imagesize['mime'], $output_format ) &&
     307                $output_format[ $imagesize['mime'] ] !== $imagesize['mime']
     308            ) {
     309                $convert = true;
     310            }
     311        }
     312
     313        if ( $scale_down || $convert ) {
    295314            $editor = wp_get_image_editor( $file );
    296315
     
    300319            }
    301320
    302             // Resize the image.
    303             $resized = $editor->resize( $threshold, $threshold );
     321            if ( $scale_down ) {
     322                // Resize the image. This will also convet it if needed.
     323                $resized = $editor->resize( $threshold, $threshold );
     324            } elseif ( $convert ) {
     325                // The image will be converted (if possible) when saved.
     326                $resized = true;
     327            }
     328
    304329            $rotated = null;
    305330
     
    307332            if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
    308333                $resized = $editor->maybe_exif_rotate();
    309                 $rotated = $resized;
     334                $rotated = $resized; // bool true or WP_Error
    310335            }
    311336
     
    315340                 * This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
    316341                 */
    317                 $saved = $editor->save( $editor->generate_filename( 'scaled' ) );
     342                if ( $scale_down ) {
     343                    $saved = $editor->save( $editor->generate_filename( 'scaled' ) );
     344                } else {
     345                    $saved = $editor->save();
     346                }
    318347
    319348                if ( ! is_wp_error( $saved ) ) {
Note: See TracChangeset for help on using the changeset viewer.