Make WordPress Core


Ignore:
Timestamp:
09/30/2024 11:54:49 PM (8 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.