Make WordPress Core

Changeset 26645


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.

Location:
trunk/src/wp-includes
Files:
3 edited

Legend:

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

    r25728 r26645  
    7878     * @access protected
    7979     *
    80      * @return boolean|\WP_Error
     80     * @return boolean|WP_Error True if loaded successfully; WP_Error on failure.
    8181     */
    8282    public function load() {
     
    8787            return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
    8888
     89        /**
     90         * Filter the memory limit allocated for image manipulation.
     91         *
     92         * @since 3.5.0
     93         *
     94         * @param int Maximum memory limit to allocate (default is WP_MAX_MEMORY_LIMIT)
     95         */
    8996        // Set artificially high because GD uses uncompressed images in memory
    9097        @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     98
    9199        $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
    92100
     
    106114        $this->mime_type = $size['mime'];
    107115
    108         return true;
     116        return $this->set_quality( $this->quality );
    109117    }
    110118
     
    362370        }
    363371        elseif ( 'image/jpeg' == $mime_type ) {
    364             if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, apply_filters( 'jpeg_quality', $this->quality, 'image_resize' ) ) ) )
     372            if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->quality ) ) )
    365373                return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
    366374        }
     
    374382        @ chmod( $filename, $perms );
    375383
     384        /**
     385         * Filter the name of the saved image file.
     386         *
     387         * @since 2.6.0
     388         *
     389         * @param string $filename Name of the file.
     390         */
    376391        return array(
    377             'path' => $filename,
    378             'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
    379             'width' => $this->size['width'],
    380             'height' => $this->size['height'],
    381             'mime-type'=> $mime_type,
     392            'path'      => $filename,
     393            'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
     394            'width'     => $this->size['width'],
     395            'height'    => $this->size['height'],
     396            'mime-type' => $mime_type,
    382397        );
    383398    }
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r25728 r26645  
    120120            return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
    121121
     122        /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
    122123        // Even though Imagick uses less PHP memory than GD, set higher limit for users that have low PHP.ini limits
    123124        @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     
    143144                return $updated_size;
    144145
    145         return $this->set_quality();
     146        return $this->set_quality( $this->quality );
    146147    }
    147148
     
    153154     *
    154155     * @param int $quality Compression Quality. Range: [1,100]
    155      * @return boolean|WP_Error
     156     * @return boolean|WP_Error True if set successfully; WP_Error on failure.
    156157     */
    157158    public function set_quality( $quality = null ) {
    158         if ( !$quality )
     159        $quality_result = parent::set_quality( $quality );
     160        if ( is_wp_error( $quality_result ) ) {
     161            return $quality_result;
     162        } else {
    159163            $quality = $this->quality;
    160 
    161         try {
    162             if( 'image/jpeg' == $this->mime_type ) {
    163                 $this->image->setImageCompressionQuality( apply_filters( 'jpeg_quality', $quality, 'image_resize' ) );
     164        }
     165
     166        try {
     167            if ( 'image/jpeg' == $this->mime_type ) {
     168                $this->image->setImageCompressionQuality( $quality );
    164169                $this->image->setImageCompression( imagick::COMPRESSION_JPEG );
    165170            }
     
    172177        }
    173178
    174         return parent::set_quality( $quality );
     179        return true;
    175180    }
    176181
     
    448453        @ chmod( $filename, $perms );
    449454
     455        /** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
    450456        return array(
    451             'path' => $filename,
    452             'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
    453             'width' => $this->size['width'],
    454             'height' => $this->size['height'],
     457            'path'      => $filename,
     458            'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
     459            'width'     => $this->size['width'],
     460            'height'    => $this->size['height'],
    455461            'mime-type' => $mime_type,
    456462        );
  • 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.