Make WordPress Core


Ignore:
Timestamp:
07/21/2022 06:01:01 PM (2 years ago)
Author:
adamsilverstein
Message:

Media: enable generating multiple mime types for image uploads; specifically WebP versions for JPEG images by default.

This changeset adds the capability for core media uploads to generate sub sized images in more than a single mime type. The output formats for each mime type can be controlled through a filter. WebP is used as an additional output format for JPEG images by default to improve front end performance.

When generating additional mime types, only images which are smaller than the respective original are retained. By default, additional mime type images are only generated for the built-in core image sizes and any custom sizes that have opted in.

Image meta is updated with a new 'sources' array containing file details for each mime type. Each image size in the 'sizes' array also gets a new 'sources' array that contains the image file details for each mime type.

This change also increases image upload retries to accommodate additional image sizes. It also adds a $mime_type parameter to the wp_get_missing_image_subsizes function and filter.

This change adds three new filters to enable full control of secondary mime image generation and output:

  • A new filter wp_image_sizes_with_additional_mime_type_support that filters the sizes that support secondary mime type output. Developers can use this to control the output of additional mime type sub-sized images on a per size basis.
  • A new filter wp_upload_image_mime_transforms that filters the output mime types for a given input mime type. Developers can use this to control generation of additional mime types for a given input mime type or even override the original mime type.
  • A new filter wp_content_image_mimes which controls image mime type output selection and order for frontend content. Developers can use this to control the mime type output preference order for content images. Content images inserted from the media library will use the available image versions based on the order from this filter.

Thanks to the many contributors who helped develop, test and give feedback on this feature.

A haiku to summarize:

Upload a JPEG
Images of all sizes
Output as WebPs

Props flixos90, MatthiasReinholz, studiolxv, markhowellsmead, eatingrules, pbiron, mukesh27, joegrainger, mehulkaklotar, tweetythierry, akshitsethi, peterwilsoncc, eugenemanuilov, mitogh, shetheliving, clarkeemily, codekraft, mikeschroder, clorith, kasparsd, spacedmonkey, trevorpfromsandee, jb510, scofennellgmailcom, seedsca, cagsmith, karinclimber, dainemawer, baxbridge, grapplerulrich, sobatkras, chynnabenton, tonylocalword, barneydavey, kwillmorth, garymatthews919, olliejones, imarkinteractive, jeffpaul, feastdesignco, webbeetle, masteradhoc.

See #55443.

File:
1 edited

Legend:

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

    r53547 r53751  
    334334    protected function get_output_format( $filename = null, $mime_type = null ) {
    335335        $new_ext = null;
     336
     337        // If no mime type is passed but output mime type is set, use that.
     338        if ( ! $mime_type && ! empty( $this->output_mime_type ) ) {
     339            $mime_type = $this->output_mime_type;
     340        }
    336341
    337342        // By default, assume specified type takes priority.
     
    426431
    427432    /**
    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
     433     * Builds an output filename based on current file, and adding proper suffix.
     434     *
     435     * @since 3.5.0
     436     * @since 6.1.0 Skips adding a suffix when set to an empty string. When the
     437     *              file extension being generated doesn't match the image file extension,
     438     *              add the extension to the suffix
     439     *
     440     * @param string $suffix    Optional. Suffix to add to the filename. The default null
     441     *                          will result in a 'widthxheight' suffix. Passing
     442     *                          an empty string will result in no suffix.
     443     * @param string $dest_path Optional. The path to save the file to. The default null
     444     *                          will use the image file path.
     445     * @param string $extension Optional. The file extension to use. The default null
     446     *                          will use the image file extension.
     447     * @return string filename The generated file name.
    436448     */
    437449    public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
    438450        // $suffix will be appended to the destination filename, just before the extension.
    439         if ( ! $suffix ) {
     451        if ( null === $suffix ) {
    440452            $suffix = $this->get_suffix();
    441453        }
     
    458470        }
    459471
    460         return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
     472        if ( empty( $suffix ) ) {
     473            $suffix = '';
     474        } else {
     475            $suffix = "-{$suffix}";
     476        }
     477
     478        // When the file extension being generated doesn't match the image file extension,
     479        // add the extension to the suffix to ensure a unique file name. Prevents
     480        // name conflicts when a single image type can have multiple extensions,
     481        // eg. .jpg, .jpeg and .jpe are all valid JPEG extensions.
     482        if ( ! empty( $extension ) && $extension !== $ext ) {
     483            $suffix .= "-{$ext}";
     484        }
     485
     486        return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
    461487    }
    462488
     
    638664        return wp_get_default_extension_for_mime_type( $mime_type );
    639665    }
     666
     667    /**
     668     * Set the editor output mime type, useful when outputting alternate mime types.
     669     *
     670     * Track that the mime type is set with the mime type set flag.
     671     *
     672     * @since 6.1.0
     673     *
     674     * @param string $output_mime_type The mime type to set.
     675     */
     676    public function set_output_mime_type( $output_mime_type ) {
     677        $this->output_mime_type = $output_mime_type;
     678    }
     679
     680    /**
     681     * Reset the mime type to the original file mime type.
     682     *
     683     * Reset the mime type set flag.
     684     *
     685     * @since 6.1.0
     686     */
     687    public function reset_output_mime_type() {
     688        $this->output_mime_type = $this->mime_type;
     689    }
    640690}
    641 
Note: See TracChangeset for help on using the changeset viewer.