Make WordPress Core

Ticket #56442: 56442.diff

File 56442.diff, 5.1 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..225fcfa5ab 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                $this->get_output_format( $this->file, $this->mime_type );
     265
    264266                // Use the output mime type if present. If not, fall back to the input/initial mime type.
    265267                $mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;
    266268                // Get the default quality setting for the mime type.
  • tests/phpunit/tests/image/editor.php

    diff --git tests/phpunit/tests/image/editor.php tests/phpunit/tests/image/editor.php
    index d478dd6bf1..50ccbfa2d4 100644
    class Tests_Image_Editor extends WP_Image_UnitTestCase { 
    160160                // Quality should change to the output format's value as filtered above.
    161161                // A JPEG image will be converted to WEBP whose quialty should be 42.
    162162                $editor->save();
    163                 $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.' );
    164164
    165165                // After removing the conversion the quality setting should reset to the filtered value for the original image type, JPEG.
    166166                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..c199f0aeeb 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                $attachment_id = self::factory()->attachment->create_object(
     3775                        $file,
     3776                        0,
     3777                        array(
     3778                                'post_mime_type' => 'image/jpeg',
     3779                        )
     3780                );
     3781
     3782                add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
     3783
     3784                // Generate all sizes as JPEGs.
     3785                $jpeg_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
     3786                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) );
     3787
     3788                // Generate all sizes as WebP.
     3789                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
     3790                $webp_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
     3791                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
     3792
     3793                // The main (scaled) image: the JPEG should be smaller than the WebP.
     3794                $this->assertLessThan( $webp_sizes['filesize'], $jpeg_sizes['filesize'] );
     3795
     3796                // Sub-sizes: for each size, the JPEGs should be smaller than the WebP.
     3797                $sizes_to_compare = array_intersect_key( $jpeg_sizes['sizes'], $webp_sizes['sizes'] );
     3798                foreach ( $sizes_to_compare as $size => $size_data ) {
     3799                        $this->assertLessThan( $webp_sizes['sizes'][ $size ]['filesize'], $jpeg_sizes['sizes'][ $size ]['filesize'] );
     3800                }
     3801
     3802                // Cleanup.
     3803                remove_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low_jpeg' ), 10, 2 );
     3804                remove_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
     3805        }
     3806
     3807        /**
     3808         * Add threshold to create a `-scaled` output image for testing.
     3809         */
     3810        public function add_big_image_size_threshold() {
     3811                return 1000;
     3812        }
     3813
     3814        /**
     3815         * Output JPEG files.
     3816         */
     3817        public function image_editor_output_jpeg() {
     3818                return array( 'image/jpeg' => 'image/jpeg' );
     3819        }
     3820
     3821        /**
     3822         * Output WebP files.
     3823         */
     3824        public function image_editor_output_webp() {
     3825                return array( 'image/jpeg' => 'image/webp' );
     3826        }
     3827
     3828        /**
     3829         * Changes the quality using very low quality for JPEGs and very high quality
     3830         * for WebPs, used to verify the filter is applying correctly.
     3831         *
     3832         * @param int    $quality   Default quality.
     3833         * @param string $mime_type Image mime-type.
     3834         * @return int The changed quality.
     3835         */
     3836        public function image_editor_change_quality_low_jpeg( $quality, $mime_type ) {
     3837                if ( 'image/jpeg' === $mime_type ) {
     3838                        return 1;
     3839                } elseif ( 'image/webp' === $mime_type ) {
     3840                        return 100;
     3841                } else {
     3842                        return 30;
     3843                }
     3844        }
     3845
    37513846}
    37523847
    37533848/**