Make WordPress Core

Changeset 54086


Ignore:
Timestamp:
09/06/2022 09:13:17 PM (20 months 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.

Location:
trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/admin-filters.php

    r53304 r54086  
    3737
    3838add_filter( 'media_upload_tabs', 'update_gallery_tab' );
     39
     40add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' );
    3941
    4042// Admin color schemes.
  • trunk/src/wp-admin/includes/image-edit.php

    r53546 r54086  
    918918
    919919    // Save the full-size file, also needed to create sub-sizes.
    920     if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
     920    $saved = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
     921    if ( ! $saved ) {
    921922        $return->error = esc_js( __( 'Unable to save the image.' ) );
    922923        return $return;
    923924    }
     925    $new_path = $saved['path'];
    924926
    925927    if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
  • trunk/src/wp-admin/includes/media.php

    r54071 r54086  
    38443844    }
    38453845}
     3846
     3847/**
     3848 * Filters the default image output mapping.
     3849 *
     3850 * With this filter callback, WebP image files will be generated for certain JPEG source files.
     3851 *
     3852 * @since 6.1.0
     3853 *
     3854 * @param array $output_mapping Map of mime type to output format.
     3855 * @retun array The adjusted default output mapping.
     3856 */
     3857function wp_default_image_output_mapping( $output_mapping ) {
     3858    $output_mapping['image/jpeg'] = 'image/webp';
     3859    return $output_mapping;
     3860}
  • trunk/src/wp-includes/class-wp-image-editor.php

    r54085 r54086  
    425425    }
    426426
    427     /**
    428      * Builds an output filename based on current file, and adding proper suffix
    429      *
    430      * @since 3.5.0
    431      *
    432      * @param string $suffix
    433      * @param string $dest_path
    434      * @param string $extension
    435      * @return string filename
     427        /**
     428     * Builds an output filename based on current file, and adding proper suffix.
     429     *
     430     * @since 3.5.0
     431     * @since 6.1.0 Skips adding a suffix when set to an empty string. When the
     432     *              file extension being generated doesn't match the image file extension,
     433     *              add the extension to the suffix
     434     *
     435     * @param string $suffix    Optional. Suffix to add to the filename. The default null
     436     *                          will result in a 'widthxheight' suffix. Passing
     437     *                          an empty string will result in no suffix.
     438     * @param string $dest_path Optional. The path to save the file to. The default null
     439     *                          will use the image file path.
     440     * @param string $extension Optional. The file extension to use. The default null
     441     *                          will use the image file extension.
     442     * @return string filename The generated file name.
    436443     */
    437444    public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
    438445        // $suffix will be appended to the destination filename, just before the extension.
    439         if ( ! $suffix ) {
     446        if ( null === $suffix ) {
    440447            $suffix = $this->get_suffix();
    441448        }
     
    458465        }
    459466
    460         return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
     467        if ( empty( $suffix ) ) {
     468            $suffix = '';
     469        } else {
     470            $suffix = "-{$suffix}";
     471        }
     472
     473        // When the file extension being generated doesn't match the image file extension,
     474        // add the extension to the suffix to ensure a unique file name. Prevents
     475        // name conflicts when a single image type can have multiple extensions,
     476        // eg. .jpg, .jpeg and .jpe are all valid JPEG extensions.
     477        if ( ! empty( $extension ) && $extension !== $ext ) {
     478            $suffix .= "-{$ext}";
     479        }
     480
     481        return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
    461482    }
    462483
  • trunk/tests/phpunit/tests/image/editor.php

    r54085 r54086  
    1919
    2020        require_once DIR_TESTDATA . '/../includes/mock-image-editor.php';
     21        add_filter( 'image_editor_output_format', '__return_empty_array' );
    2122
    2223        // This needs to come after the mock image editor class is loaded.
    2324        parent::set_up();
     25    }
     26
     27    /**
     28     * Tear down the class.
     29     */
     30    public function tear_down() {
     31        remove_filter( 'image_editor_output_format', '__return_empty_array' );
     32        parent::tear_down();
    2433    }
    2534
     
    227236
    228237        // Test with a suffix only.
    229         $this->assertSame( 'canola-100x50.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
     238        $this->assertSame( 'canola-100x50-jpg.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
    230239
    231240        // Combo!
    232         $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
     241        $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new-jpg.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
    233242
    234243        // Test with a stream destination.
  • trunk/tests/phpunit/tests/image/editorGd.php

    r52837 r54086  
    1818        require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
    1919
     20        add_filter( 'image_editor_output_format', '__return_empty_array' );
     21
    2022        // This needs to come after the mock image editor class is loaded.
    2123        parent::set_up();
     
    3032
    3133        $this->remove_added_uploads();
     34
     35        remove_filter( 'image_editor_output_format', '__return_empty_array' );
    3236
    3337        parent::tear_down();
  • trunk/tests/phpunit/tests/image/editorImagick.php

    r52837 r54086  
    1919        require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';
    2020
     21        add_filter( 'image_editor_output_format', '__return_empty_array' );
     22
    2123        // This needs to come after the mock image editor class is loaded.
    2224        parent::set_up();
     
    3133
    3234        $this->remove_added_uploads();
     35
     36        remove_filter( 'image_editor_output_format', '__return_empty_array' );
    3337
    3438        parent::tear_down();
  • trunk/tests/phpunit/tests/image/functions.php

    r54085 r54086  
    2626            unlink( $file );
    2727        }
     28
     29        add_filter( 'image_editor_output_format', '__return_empty_array' );
     30    }
     31
     32    /**
     33     * Tear down the class.
     34     */
     35    public function tear_down() {
     36        remove_filter( 'image_editor_output_format', '__return_empty_array' );
     37        parent::tear_down();
    2838    }
    2939
  • trunk/tests/phpunit/tests/image/intermediateSize.php

    r52389 r54086  
    66 */
    77class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
     8    /**
     9     * Set up the test fixture.
     10     */
     11    public function set_up() {
     12        add_filter( 'image_editor_output_format', '__return_empty_array' );
     13
     14        parent::set_up();
     15    }
     16
    817    public function tear_down() {
    918        $this->remove_added_uploads();
     
    1322        remove_image_size( 'false-width' );
    1423        remove_image_size( 'off-by-one' );
     24
     25        remove_filter( 'image_editor_output_format', '__return_empty_array' );
     26
    1527        parent::tear_down();
    1628    }
  • trunk/tests/phpunit/tests/image/resize.php

    r53463 r54086  
    1515
    1616        add_filter( 'wp_image_editors', array( $this, 'wp_image_editors' ) );
     17        add_filter( 'image_editor_output_format', '__return_empty_array' );
     18    }
     19
     20    /**
     21     * Tear down the class.
     22     */
     23    public function tear_down() {
     24        remove_filter( 'image_editor_output_format', '__return_empty_array' );
     25        parent::tear_down();
    1726    }
    1827
  • 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
  • trunk/tests/phpunit/tests/post/attachments.php

    r52389 r54086  
    77 */
    88class Tests_Post_Attachments extends WP_UnitTestCase {
     9    /**
     10     * Set up the test fixture.
     11     */
     12    public function set_up() {
     13        add_filter( 'image_editor_output_format', '__return_empty_array' );
     14
     15        parent::set_up();
     16    }
    917
    1018    public function tear_down() {
    1119        // Remove all uploads.
    1220        $this->remove_added_uploads();
     21        remove_filter( 'image_editor_output_format', '__return_empty_array' );
    1322        parent::tear_down();
    1423    }
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r54085 r54086  
    9494        add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
    9595        add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
     96        add_filter( 'image_editor_output_format', '__return_empty_array' );
    9697    }
    9798
     
    121122            WP_Image_Editor_Mock::$size_return = null;
    122123        }
     124
     125        remove_filter( 'image_editor_output_format', '__return_empty_array' );
    123126
    124127        parent::tear_down();
Note: See TracChangeset for help on using the changeset viewer.