Make WordPress Core


Ignore:
Timestamp:
07/21/2022 06:01:01 PM (21 months 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/post.php

    r53723 r53751  
    64826482
    64836483        foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    6484             $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );
    6485 
    6486             if ( ! empty( $intermediate_file ) ) {
    6487                 $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
    6488 
    6489                 if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
    6490                     $deleted = false;
     6484
     6485            // Check for alternate size mime types in the sizeinfo['sources'] array to delete.
     6486            if ( isset( $sizeinfo['sources'] ) && is_array( $sizeinfo['sources'] ) ) {
     6487                foreach ( $sizeinfo['sources'] as $mime => $properties ) {
     6488                    $intermediate_file = str_replace( wp_basename( $file ), $properties['file'], $file );
     6489                    if ( ! empty( $intermediate_file ) ) {
     6490                        $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     6491                        if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     6492                            $deleted = false;
     6493                        }
     6494                    }
     6495                }
     6496            } else {
     6497                // Otherwise, delete files from the sizeinfo data.
     6498                $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );
     6499
     6500                if ( ! empty( $intermediate_file ) ) {
     6501                    $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     6502
     6503                    if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     6504                        $deleted = false;
     6505                    }
    64916506                }
    64926507            }
     
    65106525    }
    65116526
     6527    // Delete the full size images from 'sources' if available, or the root file.
     6528    if ( isset( $meta['sources'] ) && is_array( $meta['sources'] ) ) {
     6529        $sources          = $meta['sources'];
     6530        $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
     6531        foreach ( $sources as $mime => $properties ) {
     6532            if ( ! is_array( $properties ) || empty( $properties['file'] ) ) {
     6533                continue;
     6534            }
     6535            $intermediate_file = str_replace( wp_basename( $file ), $properties['file'], $file );
     6536            if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     6537                $deleted = false;
     6538            }
     6539        }
     6540    } else {
     6541        if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     6542            $deleted = false;
     6543        }
     6544    }
     6545
    65126546    if ( is_array( $backup_sizes ) ) {
     6547
    65136548        $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    6514 
     6549        // Delete the root (edited) file which was not deleted above.
     6550        if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     6551            $deleted = false;
     6552        }
    65156553        foreach ( $backup_sizes as $size ) {
    6516             $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
    6517 
    6518             if ( ! empty( $del_file ) ) {
    6519                 $del_file = path_join( $uploadpath['basedir'], $del_file );
    6520 
    6521                 if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
    6522                     $deleted = false;
     6554            // Delete files from 'sources' data if available, otherwise from 'sizes' data.
     6555            if ( isset( $meta['sources'] ) && is_array( $meta['sources'] ) ) {
     6556                // Delete any backup images stored in the 'sources' array.
     6557                if ( isset( $size['sources'] ) && is_array( $size['sources'] ) ) {
     6558                    foreach ( $size['sources'] as $mime => $properties ) {
     6559                        $del_file = path_join( dirname( $meta['file'] ), $properties['file'] );
     6560                        if ( ! empty( $del_file ) ) {
     6561                            $del_file = path_join( $uploadpath['basedir'], $del_file );
     6562                            if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     6563                                $deleted = false;
     6564                            }
     6565                        }
     6566                    }
     6567                }
     6568            } else {
     6569                $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     6570
     6571                if ( ! empty( $del_file ) ) {
     6572                    $del_file = path_join( $uploadpath['basedir'], $del_file );
     6573                    if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     6574                        $deleted = false;
     6575                    }
    65236576                }
    65246577            }
    65256578        }
    6526     }
    6527 
    6528     if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
    6529         $deleted = false;
    65306579    }
    65316580
Note: See TracChangeset for help on using the changeset viewer.