Make WordPress Core

Changeset 59247


Ignore:
Timestamp:
10/17/2024 05:03:23 PM (7 weeks ago)
Author:
adamsilverstein
Message:

Media: Ensure Imagick quality is set correctly.

Fix an issue where Imagick did not properly set image quality for AVIF images. Adds a call to setCompressionQuality where we currently call setImageCompressionQuality

Props: jamesosborne, adamsilverstein, mukeshpanchal27.

Fixes: #61614.

Location:
trunk
Files:
2 edited

Legend:

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

    r59145 r59247  
    207207                case 'image/jpeg':
    208208                    $this->image->setImageCompressionQuality( $quality );
     209                    $this->image->setCompressionQuality( $quality );
    209210                    $this->image->setImageCompression( imagick::COMPRESSION_JPEG );
    210211                    break;
     
    215216                        // Use WebP lossless settings.
    216217                        $this->image->setImageCompressionQuality( 100 );
     218                        $this->image->setCompressionQuality( 100 );
    217219                        $this->image->setOption( 'webp:lossless', 'true' );
    218220                        parent::set_quality( 100 );
    219221                    } else {
    220222                        $this->image->setImageCompressionQuality( $quality );
     223                        $this->image->setCompressionQuality( $quality );
    221224                    }
    222225                    break;
     
    225228                    $this->image->setOption( 'heic:speed', 7 );
    226229                    $this->image->setImageCompressionQuality( $quality );
     230                    $this->image->setCompressionQuality( $quality );
    227231                    break;
    228232                default:
    229233                    $this->image->setImageCompressionQuality( $quality );
     234                    $this->image->setCompressionQuality( $quality );
    230235            }
    231236        } catch ( Exception $e ) {
  • trunk/tests/phpunit/tests/media.php

    r59145 r59247  
    53925392
    53935393    /**
     5394     * Test AVIF quality filters.
     5395     *
     5396     * @ticket 61614
     5397     */
     5398    public function test_quality_with_avif_conversion_file_sizes() {
     5399        $temp_dir = get_temp_dir();
     5400        $file     = $temp_dir . '/33772.jpg';
     5401        copy( DIR_TESTDATA . '/images/33772.jpg', $file );
     5402
     5403        $editor = wp_get_image_editor( $file );
     5404        // Only continue if the server supports AVIF.
     5405        if ( ! $editor->supports_mime_type( 'image/avif' ) ) {
     5406            $this->markTestSkipped( 'AVIF is not supported by the selected image editor.' );
     5407        }
     5408
     5409        $attachment_id = self::factory()->attachment->create_object(
     5410            array(
     5411                'post_mime_type' => 'image/jpeg',
     5412                'file'           => $file,
     5413            )
     5414        );
     5415
     5416        // Test sizes with AVIF images.
     5417        add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_avif' ) );
     5418        $avif_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
     5419        remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_avif' ) );
     5420
     5421        // Set the compression quality to a lower setting and test again, verifying that file sizes are all smaller.
     5422        add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_avif' ) );
     5423        add_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low' ) );
     5424        $smaller_avif_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
     5425        remove_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low' ) );
     5426        remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_avif' ) );
     5427
     5428        // Sub-sizes: for each size, the AVIF should be smaller than the JPEG.
     5429        $sizes_to_compare = array_intersect_key( $avif_sizes['sizes'], $smaller_avif_sizes['sizes'] );
     5430
     5431        foreach ( $sizes_to_compare as $size => $size_data ) {
     5432            $this->assertLessThan( $avif_sizes['sizes'][ $size ]['filesize'], $smaller_avif_sizes['sizes'][ $size ]['filesize'] );
     5433        }
     5434    }
     5435
     5436    /**
    53945437     * Test that an image size isn't generated if it matches the original image size.
    53955438     *
     
    64436486
    64446487    /**
     6488     * Output AVIF images.
     6489     */
     6490    public function image_editor_output_avif() {
     6491        return array( 'image/jpeg' => 'image/avif' );
     6492    }
     6493
     6494    /**
    64456495     * Changes the quality using very low quality for JPEGs and very high quality
    64466496     * for WebPs, used to verify the filter is applying correctly.
     
    64616511
    64626512    /**
     6513     * Output only low quality images.
     6514     */
     6515    public function image_editor_change_quality_low( $quality ) {
     6516        return 15;
     6517    }
     6518
     6519    /**
    64636520     * Change the omit loading attribute threshold value.
    64646521     *
Note: See TracChangeset for help on using the changeset viewer.