Make WordPress Core


Ignore:
Timestamp:
08/31/2021 06:57:09 PM (5 years ago)
Author:
azaozz
Message:

Media: apply the wp_editor_set_quality filter not only when loading an image in the editor but also when saving an converted image, after the mime-type of the output image has changed.

Props mikeschroder, desrosj, azaozz.
Fixes #53667.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/image/editor.php

    r51568 r51704  
    112112
    113113        /**
     114         * Test test_quality when converting image
     115         *
     116         * @ticket 6821
     117         */
     118        public function test_set_quality_with_image_conversion() {
     119                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/test-image.png' );
     120                $editor->set_mime_type( 'image/png' ); // Ensure mime-specific filters act properly.
     121
     122                // Set conversions for uploaded images.
     123                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
     124
     125                // Quality setting for the source image. For PNG the fallback default of 82 is used.
     126                $this->assertSame( 82, $editor->get_quality(), 'Default quality setting is 82.' );
     127
     128                // Quality should change to the output format's value.
     129                // A PNG image will be converted to WEBP whose quialty should be 86.
     130                $editor->save();
     131                $this->assertSame( 86, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 86.' );
     132
     133                // Removing PNG to WEBP conversion on save. Quality setting should reset to the default.
     134                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
     135                $editor->save();
     136                $this->assertSame( 82, $editor->get_quality(), 'After removing image conversion quality setting should reset to the default of 82.' );
     137
     138                unset( $editor );
     139
     140                // Set conversions for uploaded images.
     141                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
     142                // Change the quality values.
     143                add_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality' ), 10, 2 );
     144
     145                // Get a new editor to clear quality state.
     146                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/test-image.jpg' );
     147                $editor->set_mime_type( 'image/jpeg' );
     148
     149                $this->assertSame( 56, $editor->get_quality(), 'Filtered default quality for JPEG is 56.' );
     150
     151                // Quality should change to the output format's value as filtered above.
     152                // A JPEG image will be converted to WEBP whose quialty should be 42.
     153                $editor->save();
     154                $this->assertSame( 42, $editor->get_quality(), 'Image conversion from JPEG to WEBP. Filtered WEBP quality shoild be 42.' );
     155
     156                // After removing the conversion the quality setting should reset to the filtered value for the original image type, JPEG.
     157                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
     158                $editor->save();
     159                $this->assertSame(
     160                        56,
     161                        $editor->get_quality(),
     162                        'After removing image conversion the quality setting should reset to the filtered value for JPEG, 56.'
     163                );
     164
     165                remove_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality' ) );
     166        }
     167
     168        /**
     169         * Changes the output format when editing images. PNG and JPEG files
     170         * will be converted to WEBP (if the image editor in PHP supports it).
     171         *
     172         * @param array $formats
     173         *
     174         * @return array
     175         */
     176        public function image_editor_output_formats( $formats ) {
     177                $formats['image/png']  = 'image/webp';
     178                $formats['image/jpeg'] = 'image/webp';
     179                return $formats;
     180        }
     181
     182        /**
     183         * Changes the quality according to the mime-type.
     184         *
     185         * @param int    $quality   Default quality.
     186         * @param string $mime_type Image mime-type.
     187         * @return int The changed quality.
     188         */
     189        public function image_editor_change_quality( $quality, $mime_type ) {
     190                if ( 'image/jpeg' === $mime_type ) {
     191                        return 56;
     192                } elseif ( 'image/webp' === $mime_type ) {
     193                        return 42;
     194                } else {
     195                        return 30;
     196                }
     197        }
     198
     199        /**
    114200         * Test generate_filename
    115201         *
Note: See TracChangeset for help on using the changeset viewer.