Make WordPress Core


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

Media: revert the multi-mime feature.

This feature isn't quite ready to land.

Reverts r53786, r53848, r53847, r53845, r53751.

Props flixos90, azaozz, dd32.
See #55443.

File:
1 edited

Legend:

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

    r53848 r54085  
    7272
    7373/**
    74  * Compares the existing image sub-sizes (as saved in the attachment meta)
    75  * to the currently registered image sub-sizes, and returns the difference.
     74 * Compare the existing image sub-sizes (as saved in the attachment meta)
     75 * to the currently registered image sub-sizes, and return the difference.
    7676 *
    7777 * Registered sub-sizes that are larger than the image are skipped.
    7878 *
    7979 * @since 5.3.0
    80  * @since 6.1.0 The $mime_type parameter was added.
    81  *
    82  * @param int    $attachment_id The image attachment post ID.
    83  * @param string $mime_type     Optional. The mime type to check for missing sizes. Default is the primary image mime.
     80 *
     81 * @param int $attachment_id The image attachment post ID.
    8482 * @return array[] Associative array of arrays of image sub-size information for
    8583 *                 missing image sizes, keyed by image size name.
    8684 */
    87 function wp_get_missing_image_subsizes( $attachment_id, $mime_type = '' ) {
     85function wp_get_missing_image_subsizes( $attachment_id ) {
    8886    if ( ! wp_attachment_is_image( $attachment_id ) ) {
    8987        return array();
    90     }
    91 
    92     $primary_mime_type = get_post_mime_type( get_post( $attachment_id ) );
    93     if ( ! $mime_type ) {
    94         $mime_type = $primary_mime_type;
    9588    }
    9689
     
    137130     * as the image may have been used in an older post.
    138131     */
    139     $missing_sizes = array();
    140     foreach ( $possible_sizes as $size_name => $size_data ) {
    141         if ( ! isset( $image_meta['sizes'][ $size_name ] ) ) {
    142             $missing_sizes[ $size_name ] = $size_data;
    143             continue;
    144         }
    145 
    146         if ( ( isset( $size_data['mime-type'] ) && $size_data['mime-type'] === $mime_type ) || isset( $size_data['sources'][ $mime_type ] ) ) {
    147             continue;
    148         }
    149 
    150         $missing_sizes[ $size_name ] = $size_data;
    151     }
    152 
    153     // Filter secondary mime types to those sizes that are enabled.
    154     if ( $primary_mime_type !== $mime_type ) {
    155         $missing_sizes = _wp_filter_image_sizes_additional_mime_type_support( $missing_sizes, $attachment_id );
    156     }
     132    $missing_sizes = array_diff_key( $possible_sizes, $image_meta['sizes'] );
    157133
    158134    /**
     
    160136     *
    161137     * @since 5.3.0
    162      * @since 6.1.0 The $mime_type filter parameter was added.
    163138     *
    164139     * @param array[] $missing_sizes Associative array of arrays of image sub-size information for
     
    166141     * @param array   $image_meta    The image meta data.
    167142     * @param int     $attachment_id The image attachment post ID.
    168      * @param string  $mime_type     The image mime type to get missing sizes for.
    169      */
    170     return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id, $mime_type );
     143     */
     144    return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id );
    171145}
    172146
     
    176150 *
    177151 * @since 5.3.0
    178  * @since 6.1.0 Now supports additional mime types, creating the additional sub-sizes and 'full' sized images.
    179152 *
    180153 * @param int $attachment_id The image attachment post ID.
     
    195168        }
    196169    } else {
    197         // Get the primary and additional mime types to generate.
    198         list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $image_file, $attachment_id );
    199 
    200         // Generate missing 'full' image files for additional mime types.
    201         if ( ! empty( $additional_mime_types ) ) {
    202             if ( isset( $image_meta['sources'] ) ) {
    203                 $missing_mime_types = array_diff( $additional_mime_types, array_keys( $image_meta['sources'] ) );
    204             } else {
    205                 $missing_mime_types = $additional_mime_types;
    206             }
    207             if ( ! empty( $missing_mime_types ) ) {
    208                 $image_meta = _wp_make_additional_mime_types( $missing_mime_types, $image_file, $image_meta, $attachment_id );
    209             }
    210         }
    211 
    212         // Generate missing image sub-sizes for each mime type.
    213         $all_mime_types = array_merge( array( $primary_mime_type ), $additional_mime_types );
    214         foreach ( $all_mime_types as $mime_type ) {
    215             $missing_sizes = wp_get_missing_image_subsizes( $attachment_id, $mime_type );
    216 
    217             if ( empty( $missing_sizes ) ) {
    218                 continue;
    219             }
    220 
    221             // This also updates the image meta.
    222             $image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id, $mime_type );
    223         }
     170        $missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
     171
     172        if ( empty( $missing_sizes ) ) {
     173            return $image_meta;
     174        }
     175
     176        // This also updates the image meta.
     177        $image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
    224178    }
    225179
     
    269223
    270224/**
    271  * Creates image mime variations and sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
     225 * Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
    272226 *
    273227 * Intended for use after an image is uploaded. Saves/updates the image metadata after each
     
    275229 *
    276230 * @since 5.3.0
    277  * @since 6.1.0 Generates sub-sizes in alternate mime types based on the `wp_image_mime_transforms` filter.
    278231 *
    279232 * @param string $file          Full path to the image file.
     
    296249        'filesize' => wp_filesize( $file ),
    297250        'sizes'    => array(),
    298         'sources'  => array(),
    299251    );
    300252
     
    306258    }
    307259
    308     // Get the primary and additional mime types to generate.
    309     list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $file, $attachment_id );
    310 
    311     list( $editor, $resized, $rotated ) = _wp_maybe_scale_and_rotate_image( $file, $attachment_id, $imagesize, $exif_meta, $primary_mime_type );
    312     if ( is_wp_error( $editor ) ) {
    313         return $image_meta;
    314     }
    315     $suffix = _wp_get_image_suffix( $resized, $rotated );
    316 
    317     // Save image only if either it was modified or if the primary mime type is different from the original.
    318     if ( ! empty( $suffix ) || $primary_mime_type !== $imagesize['mime'] ) {
    319         $saved = $editor->save( $editor->generate_filename( $suffix ) );
    320 
    321         if ( ! is_wp_error( $saved ) ) {
    322             $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
    323 
    324             // If the image was rotated update the stored EXIF data.
    325             if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) {
    326                 $image_meta['image_meta']['orientation'] = 1;
    327             }
    328         } else {
    329             // TODO: Log errors.
    330         }
    331     }
    332 
    333     // Set 'sources' for the primary mime type.
    334     $image_meta['sources'][ $primary_mime_type ] = _wp_get_sources_from_meta( $image_meta );
    335 
    336     /*
    337      * Initial save of the new metadata.
    338      * At this point the file was uploaded and moved to the uploads directory
    339      * but the image sub-sizes haven't been created yet and the `sizes` array is empty.
    340      */
    341     wp_update_attachment_metadata( $attachment_id, $image_meta );
    342 
    343     if ( ! empty( $additional_mime_types ) ) {
    344         // Use the original file's exif_meta orientation and size information for secondary mime generation to ensure
    345         // sub-sized images are correctly scaled and rotated.
    346 
    347         // Save data.
    348         $saved_meta                = array();
    349         $saved_meta['orientation'] = $image_meta['image_meta']['orientation'];
    350         $saved_meta['width']       = $image_meta['width'];
    351         $saved_meta['height']      = $image_meta['height'];
    352 
    353         // Temporarily set the image meta to the original file's meta.
    354         $image_meta['image_meta']['orientation'] = $exif_meta['orientation'];
    355         $image_meta['width']                     = $imagesize[0];
    356         $image_meta['height']                    = $imagesize[1];
    357 
    358         $image_meta = _wp_make_additional_mime_types( $additional_mime_types, $file, $image_meta, $attachment_id );
    359 
    360         // Restore the saved meta data.
    361         $image_meta['image_meta']['orientation'] = $saved_meta['orientation'];
    362         $image_meta['width']                     = $saved_meta['width'];
    363         $image_meta['height']                    = $saved_meta['height'];
    364 
    365     }
    366 
    367     $new_sizes = wp_get_registered_image_subsizes();
    368 
    369     /**
    370      * Filters the image sizes automatically generated when uploading an image.
    371      *
    372      * @since 2.9.0
    373      * @since 4.4.0 Added the `$image_meta` argument.
    374      * @since 5.3.0 Added the `$attachment_id` argument.
    375      *
    376      * @param array $new_sizes     Associative array of image sizes to be created.
    377      * @param array $image_meta    The image meta data: width, height, file, sizes, etc.
    378      * @param int   $attachment_id The attachment post ID for the image.
    379      */
    380     $new_sizes = apply_filters( 'intermediate_image_sizes_advanced', $new_sizes, $image_meta, $attachment_id );
    381 
    382     $image_meta = _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id, $primary_mime_type );
    383 
    384     // Filter secondary mime types to those sizes that are enabled.
    385     $new_sizes = _wp_filter_image_sizes_additional_mime_type_support( $new_sizes, $attachment_id );
    386 
    387     foreach ( $additional_mime_types as $additional_mime_type ) {
    388         $image_meta = _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id, $additional_mime_type );
    389     }
    390 
    391     return $image_meta;
    392 }
    393 
    394 /**
    395  * Returns a WP_Image_Editor instance where the image file has been scaled and rotated as necessary.
    396  *
    397  * @since 6.1.0
    398  * @access private
    399  *
    400  * @param string     $file          Full path to the image file.
    401  * @param int        $attachment_id Attachment ID.
    402  * @param array      $imagesize     {
    403  *     Indexed array of the image width and height in pixels.
    404  *
    405  *     @type int $0 The image width.
    406  *     @type int $1 The image height.
    407  * }
    408  * @param array|null $exif_meta EXIF metadata if extracted from the image file.
    409  * @param string     $mime_type Output mime type.
    410  * @return array Array with three entries: The WP_Image_Editor instance, whether the image was resized, and whether the
    411  *               image was rotated (booleans). Each entry can alternatively be a WP_Error in case something went wrong.
    412  */
    413 function _wp_maybe_scale_and_rotate_image( $file, $attachment_id, $imagesize, $exif_meta, $mime_type ) {
    414     $resized = false;
    415     $rotated = false;
    416 
    417     $editor = wp_get_image_editor( $file, array( 'mime_type' => $mime_type ) );
    418     if ( is_wp_error( $editor ) ) {
    419         // This image cannot be edited.
    420         return array( $editor, $resized, $rotated );
    421     }
    422 
    423     if ( ! empty( $mime_type ) ) {
    424         $editor->set_output_mime_type( $mime_type );
    425     }
    426 
    427260    // Do not scale (large) PNG images. May result in sub-sizes that have greater file size than the original. See #48736.
    428     if ( 'image/png' !== $mime_type ) {
     261    if ( 'image/png' !== $imagesize['mime'] ) {
     262
    429263        /**
    430264         * Filters the "BIG image" threshold value.
     
    452286        // If the original image's dimensions are over the threshold,
    453287        // scale the image and use it as the "full" size.
    454         if ( $threshold && ( $imagesize[0] > $threshold || $imagesize[1] > $threshold ) ) {
     288        if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) {
     289            $editor = wp_get_image_editor( $file );
     290
     291            if ( is_wp_error( $editor ) ) {
     292                // This image cannot be edited.
     293                return $image_meta;
     294            }
     295
    455296            // Resize the image.
    456297            $resized = $editor->resize( $threshold, $threshold );
     298            $rotated = null;
    457299
    458300            // If there is EXIF data, rotate according to EXIF Orientation.
    459301            if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
    460                 $rotated = $editor->maybe_exif_rotate();
     302                $resized = $editor->maybe_exif_rotate();
     303                $rotated = $resized;
     304            }
     305
     306            if ( ! is_wp_error( $resized ) ) {
     307                // Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg".
     308                // This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
     309                $saved = $editor->save( $editor->generate_filename( 'scaled' ) );
     310
     311                if ( ! is_wp_error( $saved ) ) {
     312                    $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
     313
     314                    // If the image was rotated update the stored EXIF data.
     315                    if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) {
     316                        $image_meta['image_meta']['orientation'] = 1;
     317                    }
     318                } else {
     319                    // TODO: Log errors.
     320                }
     321            } else {
     322                // TODO: Log errors.
    461323            }
    462324        } elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
    463325            // Rotate the whole original image if there is EXIF data and "orientation" is not 1.
     326
     327            $editor = wp_get_image_editor( $file );
     328
     329            if ( is_wp_error( $editor ) ) {
     330                // This image cannot be edited.
     331                return $image_meta;
     332            }
     333
     334            // Rotate the image.
    464335            $rotated = $editor->maybe_exif_rotate();
    465         }
    466     }
    467 
    468     return array( $editor, $resized, $rotated );
    469 }
    470 
    471 /**
    472  * Gets the suffix to use for image files based on resizing and rotating.
    473  *
    474  * @since 6.1.0
    475  * @access private
    476  *
    477  * @param bool|WP_Error Whether the image was resized, or an error if resizing failed.
    478  * @param bool|WP_Error Whether the image was rotated, or an error if rotating failed.
    479  * @return string The suffix to use for the file name, or empty string if none.
    480  */
    481 function _wp_get_image_suffix( $resized, $rotated ) {
    482     if ( $resized && ! is_wp_error( $resized ) ) {
    483         // Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg".
    484         // This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
    485         return 'scaled';
    486     }
    487 
    488     if ( true === $rotated ) {
    489         // Append `-rotated` to the image file name.
    490         return 'rotated';
    491     }
    492 
    493     if ( is_wp_error( $resized ) || is_wp_error( $rotated ) ) {
    494         // TODO: Log errors.
    495     }
    496     return '';
    497 }
    498 
    499 /**
    500  * Gets a sources array element from a meta.
    501  *
    502  * @since 6.1.0
    503  * @access private
    504  *
    505  * @param array $meta The meta to get the source from.
    506  * @return array The source array element.
    507  */
    508 function _wp_get_sources_from_meta( $meta ) {
    509     return array(
    510         'file'     => isset( $meta['file'] ) ? wp_basename( $meta['file'] ) : '',
    511         'filesize' => isset( $meta['filesize'] ) ? $meta['filesize'] : wp_filesize( $meta['path'] ),
    512     );
     336
     337            if ( true === $rotated ) {
     338                // Append `-rotated` to the image file name.
     339                $saved = $editor->save( $editor->generate_filename( 'rotated' ) );
     340
     341                if ( ! is_wp_error( $saved ) ) {
     342                    $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
     343
     344                    // Update the stored EXIF data.
     345                    if ( ! empty( $image_meta['image_meta']['orientation'] ) ) {
     346                        $image_meta['image_meta']['orientation'] = 1;
     347                    }
     348                } else {
     349                    // TODO: Log errors.
     350                }
     351            }
     352        }
     353    }
     354
     355    /*
     356     * Initial save of the new metadata.
     357     * At this point the file was uploaded and moved to the uploads directory
     358     * but the image sub-sizes haven't been created yet and the `sizes` array is empty.
     359     */
     360    wp_update_attachment_metadata( $attachment_id, $image_meta );
     361
     362    $new_sizes = wp_get_registered_image_subsizes();
     363
     364    /**
     365     * Filters the image sizes automatically generated when uploading an image.
     366     *
     367     * @since 2.9.0
     368     * @since 4.4.0 Added the `$image_meta` argument.
     369     * @since 5.3.0 Added the `$attachment_id` argument.
     370     *
     371     * @param array $new_sizes     Associative array of image sizes to be created.
     372     * @param array $image_meta    The image meta data: width, height, file, sizes, etc.
     373     * @param int   $attachment_id The attachment post ID for the image.
     374     */
     375    $new_sizes = apply_filters( 'intermediate_image_sizes_advanced', $new_sizes, $image_meta, $attachment_id );
     376
     377    return _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id );
    513378}
    514379
     
    520385 *
    521386 * @since 5.3.0
    522  * @since 6.1.0 The $mime_type parameter was added.
    523387 * @access private
    524388 *
    525  * @param array  $new_sizes       Array defining what sizes to create.
    526  * @param string $file            Full path to the image file.
    527  * @param array  $image_meta      The attachment meta data array.
    528  * @param int    $attachment_id   Attachment ID to process.
    529  * @param string $mime_type       Optional. The mime type to check for missing sizes. Default is the image mime of $file.
     389 * @param array  $new_sizes     Array defining what sizes to create.
     390 * @param string $file          Full path to the image file.
     391 * @param array  $image_meta    The attachment meta data array.
     392 * @param int    $attachment_id Attachment ID to process.
    530393 * @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
    531394 */
    532 function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id, $mime_type = '' ) {
     395function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
    533396    if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
    534397        // Not an image attachment.
    535398        return array();
    536     }
    537 
    538     if ( ! $mime_type ) {
    539         $mime_type = wp_get_image_mime( $file );
    540399    }
    541400
     
    549408             */
    550409            if ( array_key_exists( $size_name, $new_sizes ) ) {
    551                 // Unset the size if it is either the required mime type already exists either as main mime type or
    552                 // within sources.
    553                 if ( $size_meta['mime-type'] === $mime_type || isset( $size_meta['sources'][ $mime_type ] ) ) {
    554                     unset( $new_sizes[ $size_name ] );
    555                 }
     410                unset( $new_sizes[ $size_name ] );
    556411            }
    557412        }
     
    579434    $new_sizes = array_filter( array_merge( $priority, $new_sizes ) );
    580435
    581     $editor = wp_get_image_editor( $file, array( 'mime_type' => $mime_type ) );
     436    $editor = wp_get_image_editor( $file );
    582437
    583438    if ( is_wp_error( $editor ) ) {
     
    585440        return $image_meta;
    586441    }
    587 
    588     $editor->set_output_mime_type( $mime_type );
    589442
    590443    // If stored EXIF data exists, rotate the source image before creating sub-sizes.
     
    605458            } else {
    606459                // Save the size meta value.
    607                 if ( ! isset( $image_meta['sizes'][ $new_size_name ] ) ) {
    608                     $image_meta['sizes'][ $new_size_name ] = $new_size_meta;
    609                 } else {
    610                     // Remove any newly generated images that are larger than the primary mime type.
    611                     $new_size     = isset( $new_size_meta['filesize'] ) ? $new_size_meta['filesize'] : 0;
    612                     $primary_size = isset( $image_meta['sizes'][ $new_size_name ]['filesize'] ) ? $image_meta['sizes'][ $new_size_name ]['filesize'] : 0;
    613 
    614                     if ( $new_size && $primary_size && $new_size >= $primary_size ) {
    615                         wp_delete_file( dirname( $file ) . '/' . $new_size_meta['file'] );
    616                         continue;
    617                     }
    618                 }
    619                 if ( ! isset( $image_meta['sizes'][ $new_size_name ]['sources'] ) ) {
    620                     $image_meta['sizes'][ $new_size_name ]['sources'] = array();
    621                 }
    622                 $image_meta['sizes'][ $new_size_name ]['sources'][ $mime_type ] = _wp_get_sources_from_meta( $new_size_meta );
     460                $image_meta['sizes'][ $new_size_name ] = $new_size_meta;
    623461                wp_update_attachment_metadata( $attachment_id, $image_meta );
    624462            }
     
    629467
    630468        if ( ! empty( $created_sizes ) ) {
    631             foreach ( $created_sizes as $created_size_name => $created_size_meta ) {
    632 
    633                 // Primary mime type is set in 'sizes' array.
    634                 if ( ! isset( $image_meta['sizes'][ $created_size_name ] ) ) {
    635                     $image_meta['sizes'][ $created_size_name ] = $created_size_meta;
    636                 } else {
    637                     // Remove any newly generated images that are larger than the primary mime type.
    638                     $new_size     = isset( $created_size_meta['filesize'] ) ? $created_size_meta['filesize'] : 0;
    639                     $primary_size = isset( $image_meta['sizes'][ $created_size_name ]['filesize'] ) ? $image_meta['sizes'][ $created_size_name ]['filesize'] : 0;
    640 
    641                     if ( $new_size && $primary_size && $new_size >= $primary_size ) {
    642                         wp_delete_file( dirname( $file ) . '/' . $created_size_meta['file'] );
    643                         continue;
    644                     }
    645                 }
    646                 if ( ! isset( $image_meta['sizes'][ $created_size_name ]['sources'] ) ) {
    647                     $image_meta['sizes'][ $created_size_name ]['sources'] = array();
    648                 }
    649                 $image_meta['sizes'][ $created_size_name ]['sources'][ $mime_type ] = _wp_get_sources_from_meta( $created_size_meta );
    650             }
    651             wp_update_attachment_metadata( $attachment_id, $image_meta );
    652         }
    653     }
    654 
    655     return $image_meta;
    656 }
    657 
    658 /**
    659  * Filters the list of image size objects that support secondary mime type output.
    660  *
    661  * @since 6.1.0
    662  *
    663  * @param array $sizes         Associative array of image sizes.
    664  * @param int   $attachment_id Attachment ID.
    665  * @return array $sizes Filtered $sizes with only those that support secondary mime type output.
    666  */
    667 function _wp_filter_image_sizes_additional_mime_type_support( $sizes, $attachment_id ) {
    668 
    669     // Include only the core sizes that do not rely on add_image_size(). Additional image sizes are opt-in.
    670     $enabled_sizes = array(
    671         'thumbnail'      => true,
    672         'medium'         => true,
    673         'medium_large'   => true,
    674         'large'          => true,
    675         'post-thumbnail' => true,
    676     );
    677 
    678     /**
    679      * Filter the sizes that support secondary mime type output. Developers can use this
    680      * to control the output of additional mime type sub-sized images.
    681      *
    682      * @since 6.1.0
    683      *
    684      * @param array $enabled_sizes Map of size names and whether they support secondary mime type output.
    685      * @param int   $attachment_id Attachment ID.
    686      */
    687     $enabled_sizes = apply_filters( 'wp_image_sizes_with_additional_mime_type_support', $enabled_sizes, $attachment_id );
    688 
    689     // Filter supported sizes to only include enabled sizes.
    690     return array_intersect_key( $sizes, array_filter( $enabled_sizes ) );
    691 }
    692 
    693 /**
    694  * Low-level function to create full-size images in additional mime types.
    695  *
    696  * Updates the image meta after each mime type image is created.
    697  *
    698  * @since 6.1.0
    699  * @access private
    700  *
    701  * @param array  $new_mime_types Array defining what mime types to create.
    702  * @param string $file           Full path to the image file.
    703  * @param array  $image_meta     The attachment meta data array.
    704  * @param int    $attachment_id  Attachment ID to process.
    705  * @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
    706  */
    707 function _wp_make_additional_mime_types( $new_mime_types, $file, $image_meta, $attachment_id ) {
    708     $imagesize          = array(
    709         $image_meta['width'],
    710         $image_meta['height'],
    711     );
    712     $exif_meta          = isset( $image_meta['image_meta'] ) ? $image_meta['image_meta'] : null;
    713     $original_file_size = isset( $image_meta['filesize'] ) ? $image_meta['filesize'] : wp_filesize( $file );
    714 
    715     foreach ( $new_mime_types as $mime_type ) {
    716         list( $editor, $resized, $rotated ) = _wp_maybe_scale_and_rotate_image( $file, $attachment_id, $imagesize, $exif_meta, $mime_type );
    717         if ( is_wp_error( $editor ) ) {
    718             // The image cannot be edited.
    719             continue;
    720         }
    721 
    722         $suffix    = _wp_get_image_suffix( $resized, $rotated );
    723         $extension = wp_get_default_extension_for_mime_type( $mime_type );
    724 
    725         $saved = $editor->save( $editor->generate_filename( $suffix, null, $extension ) );
    726 
    727         if ( is_wp_error( $saved ) ) {
    728             // TODO: Log errors.
    729         } else {
    730             // If the saved image is larger than the original, discard it.
    731             $filesize = isset( $saved['filesize'] ) ? $saved['filesize'] : wp_filesize( $saved['path'] );
    732             if ( $filesize && $original_file_size && $filesize > $original_file_size ) {
    733                 wp_delete_file( $saved['path'] );
    734                 continue;
    735             }
    736             $image_meta['sources'][ $mime_type ] = _wp_get_sources_from_meta( $saved );
     469            $image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes );
    737470            wp_update_attachment_metadata( $attachment_id, $image_meta );
    738471        }
     
    828561                 *     Information about the newly-uploaded file.
    829562                 *
    830                  *     @type string $file Filename of the newly-uploaded file.
    831                  *     @type string $url  URL of the uploaded file.
    832                  *     @type string $type File type.
     563                 *     @type string $file  Filename of the newly-uploaded file.
     564                 *     @type string $url   URL of the uploaded file.
     565                 *     @type string $type  File type.
    833566                 * }
    834567                 */
     
    898631
    899632                    // Create sub-sizes saving the image meta after each.
    900                     $metadata = _wp_make_subsizes( $merged_sizes, $image_file, $metadata, $attachment_id, '' );
     633                    $metadata = _wp_make_subsizes( $merged_sizes, $image_file, $metadata, $attachment_id );
    901634                }
    902635            }
     
    14251158    return $dst_file;
    14261159}
    1427 
    1428 /**
    1429  * Returns an array with the list of valid mime types that a specific mime type should be converted into.
    1430  * For example an `image/jpeg` should be converted into an `image/jpeg` and `image/webp`. The first type
    1431  * is considered the primary output type for this image.
    1432  *
    1433  * Called for each uploaded image to determine the list of mime types that should be converted into. Then,
    1434  * called again for each image size as they are generated to check if the image should be converted into the mime type
    1435  * for that size.
    1436  *
    1437  * @since 6.1.0
    1438  *
    1439  * @param int    $attachment_id The attachment ID.
    1440  * @return array An array of valid mime types, where the key is the source file mime type and the list of mime types to
    1441  *               generate.
    1442  */
    1443 function wp_upload_image_mime_transforms( $attachment_id ) {
    1444     $default_image_mime_transforms = array(
    1445         'image/jpeg' => array( 'image/jpeg', 'image/webp' ),
    1446         'image/webp' => array( 'image/webp', 'image/jpeg' ),
    1447     );
    1448     $image_mime_transforms         = $default_image_mime_transforms;
    1449 
    1450     /**
    1451      * Filters the output mime types for a given input mime type and image size.
    1452      *
    1453      * @since 6.1.0
    1454      *
    1455      * @param array $image_mime_transforms A map with the valid mime transforms where the key is the source file mime type
    1456      *                                      and the value is one or more mime file types to generate.
    1457      * @param int   $attachment_id         The ID of the attachment where the hook was dispatched.
    1458      */
    1459     $image_mime_transforms = apply_filters( 'wp_upload_image_mime_transforms', $image_mime_transforms, $attachment_id );
    1460 
    1461     if ( ! is_array( $image_mime_transforms ) ) {
    1462         return $default_image_mime_transforms;
    1463     }
    1464 
    1465     return array_map(
    1466         function( $transforms_list ) {
    1467             return (array) $transforms_list;
    1468         },
    1469         $image_mime_transforms
    1470     );
    1471 }
    1472 
    1473 /**
    1474  * Extracts the primary and additional mime output types for an image from the $image_mime_transforms.
    1475  *
    1476  * @since 6.1.0
    1477  * @access private
    1478  *
    1479  * @param string $file          Full path to the image file.
    1480  * @param int    $attachment_id Attachment ID to process.
    1481  * @return array An array with two entries, the primary mime type and the list of additional mime types.
    1482  */
    1483 function _wp_get_primary_and_additional_mime_types( $file, $attachment_id ) {
    1484     $image_mime_transforms = wp_upload_image_mime_transforms( $attachment_id );
    1485     $original_mime_type    = wp_get_image_mime( $file );
    1486     $output_mime_types     = isset( $image_mime_transforms[ $original_mime_type ] ) ? $image_mime_transforms[ $original_mime_type ] : array( $original_mime_type );
    1487 
    1488     // Exclude any output mime types that the system doesn't support.
    1489     $output_mime_types = array_values(
    1490         array_filter(
    1491             $output_mime_types,
    1492             function( $mime_type ) {
    1493                 return wp_image_editor_supports(
    1494                     array(
    1495                         'mime_type' => $mime_type,
    1496                     )
    1497                 );
    1498             }
    1499         )
    1500     );
    1501 
    1502     // Handle an empty value for $output_mime_types: only output the original type.
    1503     if ( empty( $output_mime_types ) ) {
    1504         return array( $original_mime_type, array() );
    1505     }
    1506 
    1507     // Use original mime type as primary mime type, or alternatively the first one.
    1508     $primary_mime_type_key = array_search( $original_mime_type, $output_mime_types, true );
    1509     if ( false === $primary_mime_type_key ) {
    1510         $primary_mime_type_key = 0;
    1511     }
    1512     // Split output mime types into primary mime type and additional mime types.
    1513     $additional_mime_types     = $output_mime_types;
    1514     list( $primary_mime_type ) = array_splice( $additional_mime_types, $primary_mime_type_key, 1 );
    1515 
    1516     return array(
    1517         $primary_mime_type,
    1518         $additional_mime_types,
    1519     );
    1520 }
Note: See TracChangeset for help on using the changeset viewer.