Make WordPress Core


Ignore:
Timestamp:
09/30/2024 06:37:05 PM (4 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
  • branches/6.5/src/wp-includes/class-wp-image-editor-gd.php

    r57614 r59136  
    566566            'filesize'  => wp_filesize( $filename ),
    567567        );
     568    }
     569
     570    /**
     571     * Sets Image Compression quality on a 1-100% scale. Handles WebP lossless images.
     572     *
     573     * @since 6.7.0
     574     *
     575     * @param int $quality Compression Quality. Range: [1,100]
     576     * @return true|WP_Error True if set successfully; WP_Error on failure.
     577     */
     578    public function set_quality( $quality = null ) {
     579        $quality_result = parent::set_quality( $quality );
     580        if ( is_wp_error( $quality_result ) ) {
     581            return $quality_result;
     582        } else {
     583            $quality = $this->get_quality();
     584        }
     585
     586        // Handle setting the quality for WebP lossless images, see https://php.watch/versions/8.1/gd-webp-lossless.
     587        try {
     588            if ( 'image/webp' === $this->mime_type && defined( 'IMG_WEBP_LOSSLESS' ) ) {
     589                $webp_info = wp_get_webp_info( $this->file );
     590                if ( ! empty( $webp_info['type'] ) && 'lossless' === $webp_info['type'] ) {
     591                    $quality = IMG_WEBP_LOSSLESS;
     592                    parent::set_quality( $quality );
     593                }
     594            }
     595        } catch ( Exception $e ) {
     596            return new WP_Error( 'image_quality_error', $e->getMessage() );
     597        }
     598        $this->quality = $quality;
     599        return true;
    568600    }
    569601
Note: See TracChangeset for help on using the changeset viewer.