Make WordPress Core

Changeset 59145


Ignore:
Timestamp:
09/30/2024 11:54:49 PM (2 months ago)
Author:
adamsilverstein
Message:

Media: improve support for lossless WebP.

When uploading lossless WebP images, WordPress now correctly outputs lossless WebP with both the Imagick and GD image editors.

Props: adamsilverstein, martinkrcho.
Fixes #60291.

Location:
trunk
Files:
3 edited

Legend:

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

    r58305 r59145  
    570570            'filesize'  => wp_filesize( $filename ),
    571571        );
     572    }
     573
     574    /**
     575     * Sets Image Compression quality on a 1-100% scale. Handles WebP lossless images.
     576     *
     577     * @since 6.7.0
     578     *
     579     * @param int $quality Compression Quality. Range: [1,100]
     580     * @return true|WP_Error True if set successfully; WP_Error on failure.
     581     */
     582    public function set_quality( $quality = null ) {
     583        $quality_result = parent::set_quality( $quality );
     584        if ( is_wp_error( $quality_result ) ) {
     585            return $quality_result;
     586        } else {
     587            $quality = $this->get_quality();
     588        }
     589
     590        // Handle setting the quality for WebP lossless images, see https://php.watch/versions/8.1/gd-webp-lossless.
     591        try {
     592            if ( 'image/webp' === $this->mime_type && defined( 'IMG_WEBP_LOSSLESS' ) ) {
     593                $webp_info = wp_get_webp_info( $this->file );
     594                if ( ! empty( $webp_info['type'] ) && 'lossless' === $webp_info['type'] ) {
     595                    $quality = IMG_WEBP_LOSSLESS;
     596                    parent::set_quality( $quality );
     597                }
     598            }
     599        } catch ( Exception $e ) {
     600            return new WP_Error( 'image_quality_error', $e->getMessage() );
     601        }
     602        $this->quality = $quality;
     603        return true;
    572604    }
    573605
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r59042 r59145  
    216216                        $this->image->setImageCompressionQuality( 100 );
    217217                        $this->image->setOption( 'webp:lossless', 'true' );
     218                        parent::set_quality( 100 );
    218219                    } else {
    219220                        $this->image->setImageCompressionQuality( $quality );
  • trunk/tests/phpunit/tests/media.php

    r59118 r59145  
    61046104    }
    61056105
     6106
     6107    /**
     6108     * Test WebP lossless quality is handled correctly.
     6109     *
     6110     * @ticket 60291
     6111     */
     6112    public function test_set_quality_webp_lossless() {
     6113        // Get a new editor to test that lossless WebP images are handled correctly.
     6114        $editor = wp_get_image_editor( DIR_TESTDATA . '/images/webp-lossless.webp' );
     6115
     6116        // If no editor is available, skip the test.
     6117        if ( is_wp_error( $editor ) ) {
     6118            $this->markTestSkipped( 'No editor available for lossless WebP images.' );
     6119        }
     6120
     6121        // Only test on GD when WebP lossless is supported.
     6122        if ( 'WP_Image_Editor_GD' === get_class( $editor ) && ! defined( 'IMG_WEBP_LOSSLESS' ) ) {
     6123            $this->markTestSkipped( 'No GD support available for lossless WebP images.' );
     6124        }
     6125
     6126        // Verify lossless quality is set correctly: IMG_WEBP_LOSSLESS for GD and 100 for Imagick.
     6127        if ( 'WP_Image_Editor_GD' === get_class( $editor ) ) {
     6128            $this->assertSame( IMG_WEBP_LOSSLESS, $editor->get_quality() );
     6129        } else {
     6130            $this->assertSame( 100, $editor->get_quality() );
     6131        }
     6132    }
     6133
    61066134    /**
    61076135     * Test generated markup for an image with lazy loading gets auto-sizes.
Note: See TracChangeset for help on using the changeset viewer.