Make WordPress Core


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 ) {
Note: See TracChangeset for help on using the changeset viewer.