Make WordPress Core


Ignore:
Timestamp:
11/29/2024 11:46:50 PM (7 weeks 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.