Make WordPress Core

Changeset 28879


Ignore:
Timestamp:
06/28/2014 03:49:55 AM (12 years ago)
Author:
wonderboymusic
Message:

Add ->get_quality() method to WP_Image_Editor class.

Adds unit tests.

Props markoheijnen.
Fixes #28154.

Location:
trunk
Files:
4 edited

Legend:

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

    r28640 r28879  
    115115                $this->mime_type = $size['mime'];
    116116
    117                 return $this->set_quality( $this->quality );
     117                return true;
    118118        }
    119119
    … …  
    395395                }
    396396                elseif ( 'image/jpeg' == $mime_type ) {
    397                         if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->quality ) ) )
     397                        if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) )
    398398                                return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
    399399                }
    … …  
    443443                        default:
    444444                                header( 'Content-Type: image/jpeg' );
    445                                 return imagejpeg( $this->image, null, $this->quality );
     445                                return imagejpeg( $this->image, null, $this->get_quality() );
    446446                }
    447447        }
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r28513 r28879  
    144144                                return $updated_size;
    145145
    146                 return $this->set_quality( $this->quality );
     146                return true;
    147147        }
    148148
    … …  
    161161                        return $quality_result;
    162162                } else {
    163                         $quality = $this->quality;
     163                        $quality = $this->get_quality();
    164164                }
    165165
  • trunk/src/wp-includes/class-wp-image-editor.php

    r28320 r28879  
    1717        protected $mime_type = null;
    1818        protected $default_mime_type = 'image/jpeg';
    19         protected $quality = 90;
     19        protected $quality = false;
     20        protected $default_quality = 90;
    2021
    2122        /**
    … …  
    204205
    205206        /**
     207         * Gets the Image Compression quality on a 1-100% scale.
     208         *
     209         * @since 4.0.0
     210         * @access public
     211         *
     212         * @return int $quality Compression Quality. Range: [1,100]
     213         */
     214        public function get_quality() {
     215                if ( ! $this->quality ) {
     216                        /**
     217                         * Filter the default image compression quality setting.
     218                         *
     219                         * @since 3.5.0
     220                         *
     221                         * @param int    $quality   Quality level between 1 (low) and 100 (high).
     222                         * @param string $mime_type Image mime type.
     223                         */
     224                        $quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type );
     225
     226                        if ( 'image/jpeg' == $this->mime_type ) {
     227                                /**
     228                                 * Filter the JPEG compression quality for backward-compatibility.
     229                                 *
     230                                 * The filter is evaluated under two contexts: 'image_resize', and 'edit_image',
     231                                 * (when a JPEG image is saved to file).
     232                                 *
     233                                 * @since 2.5.0
     234                                 *
     235                                 * @param int    $quality Quality level between 0 (low) and 100 (high) of the JPEG.
     236                                 * @param string $context Context of the filter.
     237                                 */
     238                                $quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
     239
     240                                if ( ! $this->set_quality( $quality ) ) {
     241                                        $this->quality = $this->default_quality;
     242                                }
     243                        }
     244                }
     245
     246                return $this->quality;
     247        }
     248
     249        /**
    206250         * Sets Image Compression quality on a 1-100% scale.
    207251         *
    … …  
    213257         */
    214258        public function set_quality( $quality = null ) {
    215                 if ( $quality == null ) {
    216                         $quality = $this->quality;
    217                 }
    218 
    219                 /**
    220                  * Filter the default image compression quality setting.
    221                  *
    222                  * @since 3.5.0
    223                  *
    224                  * @param int    $quality   Quality level between 1 (low) and 100 (high).
    225                  * @param string $mime_type Image mime type.
    226                  */
    227                 $quality = apply_filters( 'wp_editor_set_quality', $quality, $this->mime_type );
    228 
    229                 if ( 'image/jpeg' == $this->mime_type ) {
    230                         /**
    231                          * Filter the JPEG compression quality for backward-compatibility.
    232                          *
    233                          * The filter is evaluated under two contexts: 'image_resize', and 'edit_image',
    234                          * (when a JPEG image is saved to file).
    235                          *
    236                          * @since 2.5.0
    237                          *
    238                          * @param int    $quality Quality level between 0 (low) and 100 (high) of the JPEG.
    239                          * @param string $context Context of the filter.
    240                          */
    241                         $quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
    242 
    243                         // Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility.
    244                         if ( $quality == 0 ) {
    245                                 $quality = 1;
    246                         }
    247                 }
    248 
    249                 if ( ( $quality >= 1 ) && ( $quality <= 100 ) ){
     259                // Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility.
     260                if ( $quality == 0 ) {
     261                        $quality = 1;
     262                }
     263
     264                if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) {
    250265                        $this->quality = $quality;
    251266                        return true;
  • trunk/tests/phpunit/tests/image/editor.php

    r25002 r28879  
    5353                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
    5454
    55                 // Make quality readable
    56                 $property = new ReflectionProperty( $editor, 'quality' );
    57                 $property->setAccessible( true );
    58 
    5955                // Ensure set_quality works
    6056                $this->assertTrue( $editor->set_quality( 75 ) );
    61                 $this->assertEquals( 75, $property->getValue( $editor ) );
     57                $this->assertEquals( 75, $editor->get_quality() );
    6258
    6359                // Ensure the quality filter works
    … …  
    6561                add_filter( 'wp_editor_set_quality', $func );
    6662                $this->assertTrue( $editor->set_quality( 75 ) );
    67                 $this->assertEquals( 100, $property->getValue( $editor ) );
     63                $this->assertEquals( 75, $editor->get_quality() );
    6864
    6965                // Clean up
Note: See TracChangeset for help on using the changeset viewer.