WordPress.org

Make WordPress Core

Ticket #36477: 36477_2.diff

File 36477_2.diff, 2.0 KB (added by codex-m, 4 years ago)

Use getImageType() to check if the image is indexed color encoded

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

     
    2323         */
    2424        protected $image;
    2525
     26        /**
     27         * Stores the information whether the image is indexed-color encoded
     28         *
     29         * @since 4.7
     30         * @access protected
     31         * @var bool
     32         */
     33        protected $indexed_color_encoded = false;
     34       
    2635        public function __destruct() {
    2736                if ( $this->image instanceof Imagick ) {
    2837                        // we don't need the original in memory anymore
     
    303312                );
    304313
    305314                /**
     315                 * Check image type whether it's using palette (indexed-color encoded).
     316                 * Use this property when saving the resized image to preserved its original image type.
     317                 * This will reduce occurrences of having a resized image filesize very much larger than the original.
     318                 * @since 4.7
     319                 */
     320                if ( ( is_callable( array( $this->image, 'getImageType' ) ) ) && ( defined( 'imagick::IMGTYPE_PALETTE' ) ) )   {
     321                        $image_type     = $this->image->getImageType();
     322                        if ( imagick::IMGTYPE_PALETTE === $image_type ) {
     323                                $this->indexed_color_encoded    = true;
     324                        }
     325                }
     326               
     327                /**
    306328                 * Set the filter value if '$filter_name' name is in our whitelist and the related
    307329                 * Imagick constant is defined or fall back to our default filter.
    308330                 */
     
    620642                        // Store initial Format
    621643                        $orig_format = $this->image->getImageFormat();
    622644
     645                        // Restore image type when saving the image to preserved original colors and optimized its file size.
     646                        if ( ( is_callable( array( $this->image, 'getImageType' ) ) ) && ( true === $this->indexed_color_encoded ) && ( defined( 'imagick::IMGTYPE_PALETTE' ) ) ) {
     647                                $this->image->setImageType( imagick::IMGTYPE_PALETTE );
     648                        }
     649                       
    623650                        $this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) );
    624651                        $this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) );
    625652