Make WordPress Core

Ticket #28154: 28154.2.diff

File 28154.2.diff, 6.5 KB (added by markoheijnen, 11 years ago)

Updated unit test.

  • src/wp-includes/class-wp-image-editor-gd.php

     
    114114                $this->update_size( $size[0], $size[1] );
    115115                $this->mime_type = $size['mime'];
    116116
    117                 return $this->set_quality( $this->quality );
     117                return true;
    118118        }
    119119
    120120        /**
     
    387387                                return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
    388388                }
    389389                elseif ( 'image/jpeg' == $mime_type ) {
    390                         if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->quality ) ) )
     390                        if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) )
    391391                                return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
    392392                }
    393393                else {
     
    435435                                return imagegif( $this->image );
    436436                        default:
    437437                                header( 'Content-Type: image/jpeg' );
    438                                 return imagejpeg( $this->image, null, $this->quality );
     438                                return imagejpeg( $this->image, null, $this->get_quality() );
    439439                }
    440440        }
    441441
  • src/wp-includes/class-wp-image-editor-imagick.php

     
    143143                if ( is_wp_error( $updated_size ) )
    144144                                return $updated_size;
    145145
    146                 return $this->set_quality( $this->quality );
     146                return true;
    147147        }
    148148
    149149        /**
     
    160160                if ( is_wp_error( $quality_result ) ) {
    161161                        return $quality_result;
    162162                } else {
    163                         $quality = $this->quality;
     163                        $quality = $this->get_quality();
    164164                }
    165165
    166166                try {
  • src/wp-includes/class-wp-image-editor.php

     
    1616        protected $size = null;
    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        /**
    2223         * Each instance handles a single file.
     
    203204        }
    204205
    205206        /**
    206          * Sets Image Compression quality on a 1-100% scale.
     207         * Gets the Image Compression quality on a 1-100% scale.
    207208         *
    208          * @since 3.5.0
     209         * @since 4.0.0
    209210         * @access public
    210211         *
    211          * @param int $quality Compression Quality. Range: [1,100]
    212          * @return boolean|WP_Error True if set successfully; WP_Error on failure.
     212         * @return int $quality Compression Quality. Range: [1,100]
    213213         */
    214         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 ) {
     214        public function get_quality() {
     215                if ( ! $this->quality ) {
    230216                        /**
    231                          * Filter the JPEG compression quality for backward-compatibility.
     217                         * Filter the default image compression quality setting.
    232218                         *
    233                          * The filter is evaluated under two contexts: 'image_resize', and 'edit_image',
    234                          * (when a JPEG image is saved to file).
     219                         * @since 3.5.0
    235220                         *
    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.
     221                         * @param int    $quality   Quality level between 1 (low) and 100 (high).
     222                         * @param string $mime_type Image mime type.
    240223                         */
    241                         $quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
     224                        $quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type );
    242225
    243                         // Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility.
    244                         if ( $quality == 0 ) {
    245                                 $quality = 1;
     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                                }
    246243                        }
    247244                }
    248245
    249                 if ( ( $quality >= 1 ) && ( $quality <= 100 ) ){
     246                return $this->quality;
     247        }
     248
     249        /**
     250         * Sets Image Compression quality on a 1-100% scale.
     251         *
     252         * @since 3.5.0
     253         * @access public
     254         *
     255         * @param int $quality Compression Quality. Range: [1,100]
     256         * @return boolean|WP_Error True if set successfully; WP_Error on failure.
     257         */
     258        public function set_quality( $quality = null ) {
     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;
    252267                } else {
  • tests/phpunit/tests/image/editor.php

     
    4545
    4646        /**
    4747         * Test test_quality
     48         * @group marko
    4849         * @ticket 6821
    4950         */
    5051        public function test_set_quality() {
     
    5152
    5253                // Get an editor
    5354                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
    54 
    55                 // Make quality readable
    56                 $property = new ReflectionProperty( $editor, 'quality' );
    57                 $property->setAccessible( true );
    58 
     55var_dump( $editor );
    5956                // Ensure set_quality works
    6057                $this->assertTrue( $editor->set_quality( 75 ) );
    61                 $this->assertEquals( 75, $property->getValue( $editor ) );
     58                $this->assertEquals( 75, $editor->get_quality() );
    6259
    6360                // Ensure the quality filter works
    6461                $func = create_function( '', "return 100;");
    6562                add_filter( 'wp_editor_set_quality', $func );
    6663                $this->assertTrue( $editor->set_quality( 75 ) );
    67                 $this->assertEquals( 100, $property->getValue( $editor ) );
     64                $this->assertEquals( 75, $editor->get_quality() );
    6865
    6966                // Clean up
    7067                remove_filter( 'wp_editor_set_quality', $func );