Make WordPress Core

Changeset 59473


Ignore:
Timestamp:
11/29/2024 11:46:50 PM (22 months ago)
Author:
adamsilverstein
Message:

Media: improve filter to enable setting output quality by image size.

Add a new $size parameter to the wp_editor_set_quality filter. $size is an array with 'width' and 'height' keys. Developers can use this information to set image quality based on the image size.

Props adamsilverstein, joemcgill, Mte90, codekraft, birgire, azaozz, sppramodh.
Fixes #54648.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r59413 r59473  
    221221
    222222                list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
     223
     224                $this->set_quality(
     225                        null,
     226                        array(
     227                                'width'  => $dst_w,
     228                                'height' => $dst_h,
     229                        )
     230                );
    223231
    224232                $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
     
    569577         *
    570578         * @since 6.7.0
    571          *
    572          * @param int $quality Compression Quality. Range: [1,100]
     579         * @since 6.8.0 The `$dims` parameter was added.
     580         *
     581         * @param int   $quality Compression Quality. Range: [1,100]
     582         * @param array $dims    Optional. Image dimensions array with 'width' and 'height' keys.
    573583         * @return true|WP_Error True if set successfully; WP_Error on failure.
    574584         */
    575         public function set_quality( $quality = null ) {
    576                 $quality_result = parent::set_quality( $quality );
     585        public function set_quality( $quality = null, $dims = array() ) {
     586                $quality_result = parent::set_quality( $quality, $dims );
    577587                if ( is_wp_error( $quality_result ) ) {
    578588                        return $quality_result;
     
    587597                                if ( ! empty( $webp_info['type'] ) && 'lossless' === $webp_info['type'] ) {
    588598                                        $quality = IMG_WEBP_LOSSLESS;
    589                                         parent::set_quality( $quality );
     599                                        parent::set_quality( $quality, $dims );
    590600                                }
    591601                        }
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r59406 r59473  
    191191         *
    192192         * @since 3.5.0
    193          *
    194          * @param int $quality Compression Quality. Range: [1,100]
     193         * @since 6.8.0 The `$dims` parameter was added.
     194         *
     195         * @param int   $quality Compression Quality. Range: [1,100]
     196         * @param array $dims    Optional. Image dimensions array with 'width' and 'height' keys.
    195197         * @return true|WP_Error True if set successfully; WP_Error on failure.
    196198         */
    197         public function set_quality( $quality = null ) {
    198                 $quality_result = parent::set_quality( $quality );
     199        public function set_quality( $quality = null, $dims = array() ) {
     200                $quality_result = parent::set_quality( $quality, $dims );
    199201                if ( is_wp_error( $quality_result ) ) {
    200202                        return $quality_result;
     
    368370                }
    369371
     372                $this->set_quality(
     373                        null,
     374                        array(
     375                                'width'  => $dst_w,
     376                                'height' => $dst_h,
     377                        )
     378                );
     379
    370380                // Execute the resize.
    371381                $thumb_result = $this->thumbnail_image( $dst_w, $dst_h );
  • trunk/src/wp-includes/class-wp-image-editor.php

    r59406 r59473  
    241241         *
    242242         * @since 3.5.0
    243          *
    244          * @param int $quality Compression Quality. Range: [1,100]
     243         * @since 6.8.0 The `$dims` parameter was added.
     244         *
     245         * @param int   $quality Compression Quality. Range: [1,100]
     246         * @param array $dims    Optional. Image dimensions array with 'width' and 'height' keys.
    245247         * @return true|WP_Error True if set successfully; WP_Error on failure.
    246          */
    247         public function set_quality( $quality = null ) {
     248
     249         */
     250        public function set_quality( $quality = null, $dims = array() ) {
    248251                // Use the output mime type if present. If not, fall back to the input/initial mime type.
    249252                $mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;
     
    261264                         *
    262265                         * @since 3.5.0
     266                         * @since 6.8.0 Added the size parameter.
    263267                         *
    264268                         * @param int    $quality   Quality level between 1 (low) and 100 (high).
    265269                         * @param string $mime_type Image mime type.
     270                         * @param array $size {
     271                         *     Dimensions of the image.
     272                         *
     273                         *     @type int $width  The image width.
     274                         *     @type int $height The image height.
     275                         * }
    266276                         */
    267                         $quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type );
     277                        $quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size );
    268278
    269279                        if ( 'image/jpeg' === $mime_type ) {
  • trunk/tests/phpunit/tests/media.php

    r59415 r59473  
    54355435
    54365436        /**
     5437         * Test that the `wp_editor_set_quality` filter includes the dimensions in the `$dims` parameter.
     5438         *
     5439         * @ticket 54648
     5440         */
     5441        public function test_wp_editor_set_quality_includes_dimensions() {
     5442                // Before loading an image, set up the callback filter with the assertions.
     5443                add_filter( 'wp_editor_set_quality', array( $this, 'assert_dimensions_in_wp_editor_set_quality' ), 10, 3 );
     5444
     5445                $temp_dir = get_temp_dir();
     5446                $file     = $temp_dir . '/33772.jpg';
     5447                copy( DIR_TESTDATA . '/images/33772.jpg', $file );
     5448
     5449                $editor = wp_get_image_editor( $file );
     5450
     5451                $attachment_id = self::factory()->attachment->create_object(
     5452                        array(
     5453                                'post_mime_type' => 'image/jpeg',
     5454                                'file'           => $file,
     5455                        )
     5456                );
     5457
     5458                // Generate all sizes.
     5459                wp_generate_attachment_metadata( $attachment_id, $file );
     5460
     5461                // Clean up the filter.
     5462                remove_filter( 'wp_editor_set_quality', array( $this, 'assert_dimensions_in_wp_editor_set_quality' ), 10, 3 );
     5463        }
     5464
     5465        /**
     5466         * Helper callback to assert that the dimensions are included in the `$dims` parameter.
     5467         *
     5468         * @param int   $quality The quality level.
     5469         * @param array $dims    The dimensions array.
     5470         */
     5471        public function assert_dimensions_in_wp_editor_set_quality( $quality, $mime_type, $dims ) {
     5472                // Assert that the array has non empty width and height values.
     5473                $this->assertArrayHasKey( 'width', $dims );
     5474                $this->assertArrayHasKey( 'height', $dims );
     5475                $this->assertGreaterThan( 0, $dims['width'] );
     5476                $this->assertGreaterThan( 0, $dims['height'] );
     5477
     5478                return $quality;
     5479        }
     5480
     5481        /**
    54375482         * Test that an image size isn't generated if it matches the original image size.
    54385483         *
Note: See TracChangeset for help on using the changeset viewer.