Make WordPress Core

Ticket #54476: 54476.4.diff

File 54476.4.diff, 3.0 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 51c49f83d2..b4ee0b04dc 100644
    function wp_get_image_editor( $path, $args = array() ) { 
    39423942                }
    39433943        }
    39443944
     3945        $mime_type = isset( $args['mime_type'] ) ? $args['mime_type'] : wp_check_filetype( $path )['type'];
     3946
     3947        /** This filter is documented in wp-includes/class-wp-image-editor.php */
     3948        $output_format = apply_filters( 'image_editor_output_format', array(), $path, $mime_type );
     3949        if ( isset( $output_format[ $mime_type ] ) ) {
     3950                $args['output_mime_type'] = $output_format[ $mime_type ];
     3951        }
     3952
    39453953        $implementation = _wp_image_editor_choose( $args );
    39463954
    39473955        if ( $implementation ) {
    function _wp_image_editor_choose( $args = array() ) { 
    39944002         *                                'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
    39954003         */
    39964004        $implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
     4005        $supports_input  = false;
    39974006
    39984007        foreach ( $implementations as $implementation ) {
    39994008                if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
    40004009                        continue;
    40014010                }
    40024011
     4012                // Implementation should support the passed mime type.
    40034013                if ( isset( $args['mime_type'] ) &&
    40044014                        ! call_user_func(
    40054015                                array( $implementation, 'supports_mime_type' ),
    function _wp_image_editor_choose( $args = array() ) { 
    40084018                        continue;
    40094019                }
    40104020
     4021                // Implementation should support requested methods.
    40114022                if ( isset( $args['methods'] ) &&
    40124023                        array_diff( $args['methods'], get_class_methods( $implementation ) ) ) {
    40134024
    40144025                        continue;
    40154026                }
    40164027
     4028                // Implementation should ideally support the output mime type as well if set and different than the passed type.
     4029                if (
     4030                        isset( $args['mime_type'] ) &&
     4031                        isset( $args['output_mime_type'] ) &&
     4032                        $args['mime_type'] !== $args['output_mime_type'] &&
     4033                        ! call_user_func( array( $implementation, 'supports_mime_type' ), $args['output_mime_type'] )
     4034                ) {
     4035                        // This implementation supports the imput type but not the output type.
     4036                        // Keep looking to see if we can find an implementation that supports both.
     4037                        $supports_input = $implementation;
     4038                        continue;
     4039                }
     4040
     4041                // Favor the implementation that supports both input and output mime types.
    40174042                return $implementation;
    40184043        }
    40194044
    4020         return false;
     4045        return $supports_input;
    40214046}
    40224047
    40234048/**
  • tests/phpunit/tests/image/editor.php

    diff --git tests/phpunit/tests/image/editor.php tests/phpunit/tests/image/editor.php
    index c051ffec2b..9d0f674d2d 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