Make WordPress Core

Ticket #54476: 54476.5.diff

File 54476.5.diff, 3.3 KB (added by adamsilverstein, 3 years ago)
  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 065efde579..f068469e52 100644
    function wp_max_upload_size() { 
    38383838function wp_get_image_editor( $path, $args = array() ) {
    38393839        $args['path'] = $path;
    38403840
     3841        // If the mime type is not set in args, try to extract and set it from the file.
    38413842        if ( ! isset( $args['mime_type'] ) ) {
    38423843                $file_info = wp_check_filetype( $args['path'] );
    38433844
    function wp_get_image_editor( $path, $args = array() ) { 
    38483849                }
    38493850        }
    38503851
     3852        $mime_type = isset( $args['mime_type'] ) ? $args['mime_type'] : '';
     3853
     3854        /** This filter is documented in wp-includes/class-wp-image-editor.php */
     3855        $output_format = apply_filters( 'image_editor_output_format', array(), $path, $mime_type );
     3856        if ( '' !== $mime_type && isset( $output_format[ $mime_type ] ) ) {
     3857                $args['output_mime_type'] = $output_format[ $mime_type ];
     3858        }
     3859
    38513860        $implementation = _wp_image_editor_choose( $args );
    38523861
    38533862        if ( $implementation ) {
    function _wp_image_editor_choose( $args = array() ) { 
    39003909         *                                'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
    39013910         */
    39023911        $implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
     3912        $supports_input  = false;
    39033913
    39043914        foreach ( $implementations as $implementation ) {
    39053915                if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
    39063916                        continue;
    39073917                }
    39083918
     3919                // Implementation should support the passed mime type.
    39093920                if ( isset( $args['mime_type'] ) &&
    39103921                        ! call_user_func(
    39113922                                array( $implementation, 'supports_mime_type' ),
    function _wp_image_editor_choose( $args = array() ) { 
    39143925                        continue;
    39153926                }
    39163927
     3928                // Implementation should support requested methods.
    39173929                if ( isset( $args['methods'] ) &&
    39183930                        array_diff( $args['methods'], get_class_methods( $implementation ) ) ) {
    39193931
    39203932                        continue;
    39213933                }
    39223934
     3935                // Implementation should ideally support the output mime type as well if set and different than the passed type.
     3936                if (
     3937                        isset( $args['mime_type'] ) &&
     3938                        isset( $args['output_mime_type'] ) &&
     3939                        $args['mime_type'] !== $args['output_mime_type'] &&
     3940                        ! call_user_func( array( $implementation, 'supports_mime_type' ), $args['output_mime_type'] )
     3941                ) {
     3942                        // This implementation supports the imput type but not the output type.
     3943                        // Keep looking to see if we can find an implementation that supports both.
     3944                        $supports_input = $implementation;
     3945                        continue;
     3946                }
     3947
     3948                // Favor the implementation that supports both input and output mime types.
    39233949                return $implementation;
    39243950        }
    39253951
    3926         return false;
     3952        return $supports_input;
    39273953}
    39283954
    39293955/**
  • tests/phpunit/tests/image/editor.php

    diff --git tests/phpunit/tests/image/editor.php tests/phpunit/tests/image/editor.php
    index 487dad0664..95981bad82 100644
    class Tests_Image_Editor extends WP_Image_UnitTestCase { 
    126126                $this->assertSame( 82, $editor->get_quality(), 'Default quality setting is 82.' );
    127127
    128128                // Quality should change to the output format's value.
    129                 // A PNG image will be converted to WEBP whose quialty should be 86.
     129                // A PNG image will be converted to WEBP whose quality should be 86.
    130130                $editor->save();
    131131                $this->assertSame( 86, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 86.' );
    132132