Make WordPress Core


Ignore:
Timestamp:
12/04/2013 10:48:45 PM (11 years ago)
Author:
nacin
Message:

Improvements to image quality handling in the image editor classes.

props markoheijnen, DH-Shredder.
fixes #25721.

File:
1 edited

Legend:

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

    r25728 r26645  
    206206     *
    207207     * @param int $quality Compression Quality. Range: [1,100]
    208      * @return boolean
    209      */
    210     public function set_quality( $quality ) {
     208     * @return boolean|WP_Error True if set successfully; WP_Error on failure.
     209     */
     210    public function set_quality( $quality = null ) {
     211        if ( $quality == null ) {
     212            $quality = $this->quality;
     213        }
     214
    211215        /**
    212216         * Filter the default quality setting.
     
    214218         * @since 3.5.0
    215219         *
    216          * @param int $quality Quality level between 0 (low) and 100 (high).
     220         * @param int $quality Quality level between 1 (low) and 100 (high).
    217221         */
    218         $this->quality = apply_filters( 'wp_editor_set_quality', $quality );
    219 
    220         return ( (bool) $this->quality );
     222        $quality = apply_filters( 'wp_editor_set_quality', $quality, $this->mime_type );
     223
     224        /**
     225         * Filter the JPEG quality for backwards compatibilty.
     226         *
     227         * @since 2.5.0
     228         *
     229         * @param int $quality Quality level between 0 (low) and 100 (high) of the JPEG.
     230         * @param string The context of the filter.
     231         */
     232        if ( 'image/jpeg' == $this->mime_type ) {
     233            $quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
     234
     235            // Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility.
     236            if ( $quality == 0 ) {
     237                $quality = 1;
     238            }
     239        }
     240
     241        if ( ( $quality >= 1 ) && ( $quality <= 100 ) ){
     242            $this->quality = $quality;
     243            return true;
     244        } else {
     245            return new WP_Error( 'invalid_image_quality', __('Attempted to set image quality outside of the range [1,100].') );
     246        }
    221247    }
    222248
Note: See TracChangeset for help on using the changeset viewer.