Make WordPress Core


Ignore:
Timestamp:
09/06/2022 09:13:17 PM (2 years ago)
Author:
adamsilverstein
Message:

Media: Output WebP by default when uploading JPEGs.

Uploaded JPEGs will automatically be converted to WebP sub-sizes instead of JPEG, saving space and making sites faster.

The original JPEG upload is always retained and can be accessed by calling wp_get_original_image_url.

Props azaozz, flixos90.
Fixes #55443.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r54085 r54086  
    3434        $GLOBALS['_wp_additional_image_sizes'] = array();
    3535
    36         $filename       = DIR_TESTDATA . '/images/' . self::$large_filename;
     36        $filename = DIR_TESTDATA . '/images/' . self::$large_filename;
     37        add_filter( 'image_editor_output_format', '__return_empty_array' );
    3738        self::$large_id = $factory->attachment->create_upload_object( $filename );
    3839
     
    6970    public static function wpTearDownAfterClass() {
    7071        $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
     72        remove_filter( 'image_editor_output_format', '__return_empty_array' );
    7173    }
    7274
     
    36183620        remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 );
    36193621    }
     3622
     3623    /**
     3624     * Test the wp_default_image_output_mapping function.
     3625     *
     3626     * @ticket 55443
     3627     */
     3628    public function test_wp_default_image_output_mapping() {
     3629        $mapping = wp_default_image_output_mapping( array() );
     3630        $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
     3631    }
     3632
     3633    /**
     3634     * Test that wp_default_image_output_mapping doesn't overwrite existing mappings.
     3635     *
     3636     * @ticket 55443
     3637     */
     3638    public function test_wp_default_image_output_mapping_existing() {
     3639        $mapping = array( 'mime/png' => 'mime/webp' );
     3640        $mapping = wp_default_image_output_mapping( $mapping );
     3641        $this->assertSame(
     3642            array(
     3643                'mime/png'   => 'mime/webp',
     3644                'image/jpeg' => 'image/webp',
     3645            ),
     3646            $mapping
     3647        );
     3648    }
     3649
     3650    /**
     3651     * Test that the image editor default output for JPEGs is WebP.
     3652     *
     3653     * @ticket 55443
     3654     */
     3655    public function test_wp_image_editor_default_output_maps_to_webp() {
     3656        remove_filter( 'image_editor_output_format', '__return_empty_array' );
     3657
     3658        $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
     3659        $this->assertNotWPError( $editor );
     3660
     3661        $resized = $editor->resize( 100, 100, false );
     3662        $this->assertNotWPError( $resized );
     3663
     3664        $saved = $editor->save();
     3665        $this->assertNotWPError( $saved );
     3666
     3667        if ( $editor->supports_mime_type( 'image/webp' ) ) {
     3668            $this->assertSame( 'image/webp', $saved['mime-type'] );
     3669            $this->assertSame( 'canola-100x75-jpg.webp', $saved['file'] );
     3670        } else {
     3671            $this->assertSame( 'image/jpeg', $saved['mime-type'] );
     3672            $this->assertSame( 'canola-100x75.jpg', $saved['file'] );
     3673        }
     3674    }
    36203675}
    36213676
Note: See TracChangeset for help on using the changeset viewer.