Make WordPress Core

Ticket #56442: 56442.5.diff

File 56442.5.diff, 6.9 KB (added by adamsilverstein, 3 years ago)
  • src/wp-includes/class-wp-image-editor.php

    diff --git src/wp-includes/class-wp-image-editor.php src/wp-includes/class-wp-image-editor.php
    index e7070b1ca6..fed0fc16e8 100644
    abstract class WP_Image_Editor { 
    414414                        // The image will be converted when saving. Set the quality for the new mime-type if not already set.
    415415                        if ( $mime_type !== $this->output_mime_type ) {
    416416                                $this->output_mime_type = $mime_type;
    417                                 $this->set_quality();
    418417                        }
     418                        $this->set_quality();
    419419                } elseif ( ! empty( $this->output_mime_type ) ) {
    420420                        // Reset output_mime_type and quality.
    421421                        $this->output_mime_type = null;
  • tests/phpunit/tests/image/editor.php

    diff --git tests/phpunit/tests/image/editor.php tests/phpunit/tests/image/editor.php
    index 487dad0664..3b99ee3e50 100644
    class Tests_Image_Editor extends WP_Image_UnitTestCase { 
    116116         * @ticket 6821
    117117         */
    118118        public function test_set_quality_with_image_conversion() {
     119                remove_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' );
     120
    119121                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/test-image.png' );
    120122                $editor->set_mime_type( 'image/png' ); // Ensure mime-specific filters act properly.
    121123
     124                // Quality setting for the source image. For PNG the fallback default of 82 is used.
     125                $this->assertSame( 82, $editor->get_quality(), 'Default quality setting is 82.' );
     126
    122127                // Set conversions for uploaded images.
    123128                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
    124129
    125130                // Quality setting for the source image. For PNG the fallback default of 82 is used.
    126131                $this->assertSame( 82, $editor->get_quality(), 'Default quality setting is 82.' );
    127132
    128                 // Quality should change to the output format's value.
    129                 // A PNG image will be converted to WEBP whose quialty should be 86.
     133                // When saving, quality should change to the output format's value.
     134                // A PNG image will be converted to WEBP whose quality should be 86.
     135                $editor->save();
     136                $this->assertSame( 86, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 86.' );
     137
     138                // Saving again should not change the quality.
    130139                $editor->save();
    131140                $this->assertSame( 86, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 86.' );
    132141
    class Tests_Image_Editor extends WP_Image_UnitTestCase { 
    151160                // Quality should change to the output format's value as filtered above.
    152161                // A JPEG image will be converted to WEBP whose quialty should be 42.
    153162                $editor->save();
    154                 $this->assertSame( 42, $editor->get_quality(), 'Image conversion from JPEG to WEBP. Filtered WEBP quality shoild be 42.' );
     163                $this->assertSame( 42, $editor->get_quality(), 'Image conversion from JPEG to WEBP. Filtered WEBP quality should be 42.' );
    155164
    156165                // After removing the conversion the quality setting should reset to the filtered value for the original image type, JPEG.
    157166                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
  • tests/phpunit/tests/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index 7421a2bdeb..1700392f6a 100644
    EOF; 
    36213621                // Clean up the above filter.
    36223622                remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 );
    36233623        }
     3624
     3625        /**
     3626         * Test that generated files with the `image_editor_output_format` applied use the correct
     3627         * quality level based on their mime type.
     3628         *
     3629         * @ticket 56442
     3630         */
     3631        public function test_quality_with_image_conversion_file_sizes() {
     3632                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     3633                        $this->markTestSkipped( 'WebP is not supported on this system.' );
     3634                }
     3635                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) );
     3636                $temp_dir = get_temp_dir();
     3637                $file     = $temp_dir . '/33772.jpg';
     3638                copy( DIR_TESTDATA . '/images/33772.jpg', $file );
     3639
     3640                // Set JPEG output quality very low and WebP quality very high, this should force all generated WebP images to
     3641                // be larger than the the matching generated JPEGs.
     3642                add_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low_jpeg' ), 10, 2 );
     3643
     3644                $editor = wp_get_image_editor( $file );
     3645
     3646                // Verify that the selected editor actually supports WebP output.
     3647                if ( ! $editor->supports_mime_type( 'image/webp' ) ) {
     3648                        $this->markTestSkipped( 'WebP is not supported by the selected image editor.' );
     3649                }
     3650
     3651                $attachment_id = self::factory()->attachment->create_object(
     3652                        $file,
     3653                        0,
     3654                        array(
     3655                                'post_mime_type' => 'image/jpeg',
     3656                        )
     3657                );
     3658
     3659                add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
     3660
     3661                // Generate all sizes as JPEGs.
     3662                $jpeg_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
     3663                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) );
     3664
     3665                // Generate all sizes as WebP.
     3666                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
     3667                $webp_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
     3668                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
     3669
     3670                // The main (scaled) image: the JPEG should be smaller than the WebP.
     3671                $this->assertLessThan( $webp_sizes['filesize'], $jpeg_sizes['filesize'] );
     3672
     3673                // Sub-sizes: for each size, the JPEGs should be smaller than the WebP.
     3674                $sizes_to_compare = array_intersect_key( $jpeg_sizes['sizes'], $webp_sizes['sizes'] );
     3675                foreach ( $sizes_to_compare as $size => $size_data ) {
     3676                        $this->assertLessThan( $webp_sizes['sizes'][ $size ]['filesize'], $jpeg_sizes['sizes'][ $size ]['filesize'] );
     3677                }
     3678
     3679                // Cleanup.
     3680                remove_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low_jpeg' ), 10, 2 );
     3681                remove_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
     3682        }
     3683
     3684        /**
     3685         * Add threshold to create a `-scaled` output image for testing.
     3686         */
     3687        public function add_big_image_size_threshold() {
     3688                return 1000;
     3689        }
     3690
     3691        /**
     3692         * Output JPEG files.
     3693         */
     3694        public function image_editor_output_jpeg() {
     3695                return array( 'image/jpeg' => 'image/jpeg' );
     3696        }
     3697
     3698        /**
     3699         * Output WebP files.
     3700         */
     3701        public function image_editor_output_webp() {
     3702                return array( 'image/jpeg' => 'image/webp' );
     3703        }
     3704
     3705        /**
     3706         * Changes the quality using very low quality for JPEGs and very high quality
     3707         * for WebPs, used to verify the filter is applying correctly.
     3708         *
     3709         * @param int    $quality   Default quality.
     3710         * @param string $mime_type Image mime-type.
     3711         * @return int The changed quality.
     3712         */
     3713        public function image_editor_change_quality_low_jpeg( $quality, $mime_type ) {
     3714                if ( 'image/jpeg' === $mime_type ) {
     3715                        return 1;
     3716                } elseif ( 'image/webp' === $mime_type ) {
     3717                        return 100;
     3718                } else {
     3719                        return 30;
     3720                }
     3721        }
     3722
    36243723}
    36253724
    36263725/**