Make WordPress Core

Ticket #56442: 56442.3.diff

File 56442.3.diff, 7.2 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 67d1bda83f..dcfcd5738b 100644
    abstract class WP_Image_Editor { 
    261261         * @return true|WP_Error True if set successfully; WP_Error on failure.
    262262         */
    263263        public function set_quality( $quality = null ) {
     264
    264265                // Use the output mime type if present. If not, fall back to the input/initial mime type.
    265266                $mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;
    266267                // Get the default quality setting for the mime type.
    abstract class WP_Image_Editor { 
    439440                        // The image will be converted when saving. Set the quality for the new mime-type if not already set.
    440441                        if ( $mime_type !== $this->output_mime_type ) {
    441442                                $this->output_mime_type = $mime_type;
    442                                 $this->set_quality();
    443443                        }
     444                        $this->set_quality();
    444445                } elseif ( ! empty( $this->output_mime_type ) ) {
    445446                        // Reset output_mime_type and quality.
    446447                        $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 d478dd6bf1..afd1329bd4 100644
    class Tests_Image_Editor extends WP_Image_UnitTestCase { 
    125125         * @ticket 6821
    126126         */
    127127        public function test_set_quality_with_image_conversion() {
     128                remove_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' );
     129
    128130                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/test-image.png' );
    129131                $editor->set_mime_type( 'image/png' ); // Ensure mime-specific filters act properly.
    130132
     133                // Quality setting for the source image. For PNG the fallback default of 82 is used.
     134                $this->assertSame( 82, $editor->get_quality(), 'Default quality setting is 82.' );
     135
    131136                // Set conversions for uploaded images.
    132137                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
    133138
    134139                // Quality setting for the source image. For PNG the fallback default of 82 is used.
    135140                $this->assertSame( 82, $editor->get_quality(), 'Default quality setting is 82.' );
    136141
    137                 // Quality should change to the output format's value.
    138                 // A PNG image will be converted to WEBP whose quialty should be 86.
     142                // When saving, quality should change to the output format's value.
     143                // A PNG image will be converted to WEBP whose quality should be 86.
     144                $editor->save();
     145                $this->assertSame( 86, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 86.' );
     146
     147                // Saving again should not change the quality.
    139148                $editor->save();
    140149                $this->assertSame( 86, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 86.' );
    141150
    class Tests_Image_Editor extends WP_Image_UnitTestCase { 
    160169                // Quality should change to the output format's value as filtered above.
    161170                // A JPEG image will be converted to WEBP whose quialty should be 42.
    162171                $editor->save();
    163                 $this->assertSame( 42, $editor->get_quality(), 'Image conversion from JPEG to WEBP. Filtered WEBP quality shoild be 42.' );
     172                $this->assertSame( 42, $editor->get_quality(), 'Image conversion from JPEG to WEBP. Filtered WEBP quality should be 42.' );
    164173
    165174                // After removing the conversion the quality setting should reset to the filtered value for the original image type, JPEG.
    166175                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 6afb92a343..4315c9c39a 100644
    EOF; 
    37483748                        ),
    37493749                );
    37503750        }
     3751
     3752
     3753        /**
     3754         * Test that generated files with the `image_editor_output_format` applied use the correct
     3755         * quality level based on the ir mime type.
     3756         *
     3757         * @ticket 56442
     3758         */
     3759        public function test_quality_with_image_conversion_file_sizes() {
     3760                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     3761                        $this->markTestSkipped( 'WebP is not supported on this system.' );
     3762                }
     3763                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) );
     3764                $temp_dir = get_temp_dir();
     3765                $file     = $temp_dir . '/33772.jpg';
     3766                copy( DIR_TESTDATA . '/images/33772.jpg', $file );
     3767
     3768                // Set JPEG output quality very low and WebP quality very high, this should force all generated WebP images to
     3769                // be larger than the the matching generated JPEGs.
     3770                add_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low_jpeg' ), 10, 2 );
     3771
     3772                $editor = wp_get_image_editor( $file );
     3773
     3774                // Verify that the selected editor actually supports WebP output.
     3775                if ( ! $editor->supports_mime_type( 'image/webp' ) ) {
     3776                        $this->markTestSkipped( 'WebP is not supported by the selected image editor.' );
     3777                }
     3778
     3779                $attachment_id = self::factory()->attachment->create_object(
     3780                        $file,
     3781                        0,
     3782                        array(
     3783                                'post_mime_type' => 'image/jpeg',
     3784                        )
     3785                );
     3786
     3787                add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
     3788
     3789                // Generate all sizes as JPEGs.
     3790                $jpeg_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
     3791                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) );
     3792
     3793                // Generate all sizes as WebP.
     3794                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
     3795                $webp_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
     3796                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
     3797
     3798                // The main (scaled) image: the JPEG should be smaller than the WebP.
     3799                $this->assertLessThan( $webp_sizes['filesize'], $jpeg_sizes['filesize'] );
     3800
     3801                // Sub-sizes: for each size, the JPEGs should be smaller than the WebP.
     3802                $sizes_to_compare = array_intersect_key( $jpeg_sizes['sizes'], $webp_sizes['sizes'] );
     3803                foreach ( $sizes_to_compare as $size => $size_data ) {
     3804                        $this->assertLessThan( $webp_sizes['sizes'][ $size ]['filesize'], $jpeg_sizes['sizes'][ $size ]['filesize'] );
     3805                }
     3806
     3807                // Cleanup.
     3808                remove_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low_jpeg' ), 10, 2 );
     3809                remove_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
     3810        }
     3811
     3812        /**
     3813         * Add threshold to create a `-scaled` output image for testing.
     3814         */
     3815        public function add_big_image_size_threshold() {
     3816                return 1000;
     3817        }
     3818
     3819        /**
     3820         * Output JPEG files.
     3821         */
     3822        public function image_editor_output_jpeg() {
     3823                return array( 'image/jpeg' => 'image/jpeg' );
     3824        }
     3825
     3826        /**
     3827         * Output WebP files.
     3828         */
     3829        public function image_editor_output_webp() {
     3830                return array( 'image/jpeg' => 'image/webp' );
     3831        }
     3832
     3833        /**
     3834         * Changes the quality using very low quality for JPEGs and very high quality
     3835         * for WebPs, used to verify the filter is applying correctly.
     3836         *
     3837         * @param int    $quality   Default quality.
     3838         * @param string $mime_type Image mime-type.
     3839         * @return int The changed quality.
     3840         */
     3841        public function image_editor_change_quality_low_jpeg( $quality, $mime_type ) {
     3842                if ( 'image/jpeg' === $mime_type ) {
     3843                        return 1;
     3844                } elseif ( 'image/webp' === $mime_type ) {
     3845                        return 100;
     3846                } else {
     3847                        return 30;
     3848                }
     3849        }
     3850
    37513851}
    37523852
    37533853/**