@@ -492,11 +492,11 @@ function _wp_get_sources_from_meta( $meta ) {
492492 * @since 6.1.0 The $mime_type parameter was added.
493493 * @access private
494494 *
495- * @param array $new_sizes Array defining what sizes to create.
496- * @param string $file Full path to the image file.
497- * @param array $image_meta The attachment meta data array.
498- * @param int $attachment_id Attachment ID to process.
499- * @param string $mime_type Optional. The mime type to check for missing sizes. Default is the image mime of $file.
495+ * @param array $new_sizes Array defining what sizes to create.
496+ * @param string $file Full path to the image file.
497+ * @param array $image_meta The attachment meta data array.
498+ * @param int $attachment_id Attachment ID to process.
499+ * @param string $mime_type Optional. The mime type to check for missing sizes. Default is the image mime of $file.
500500 * @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
501501 */
502502function _wp_make_subsizes ( $ new_sizes , $ file , $ image_meta , $ attachment_id , $ mime_type = '' ) {
@@ -505,13 +505,21 @@ function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id, $mim
505505 return array ();
506506 }
507507
508+ $ original_mime_type = wp_get_image_mime ( $ file );
509+
508510 if ( ! $ mime_type ) {
509- $ mime_type = wp_get_image_mime ( $ file ) ;
511+ $ mime_type = $ original_mime_type ;
510512 }
511513
514+
512515 // Check if any of the new sizes already exist.
513516 if ( isset ( $ image_meta ['sizes ' ] ) && is_array ( $ image_meta ['sizes ' ] ) ) {
514517 foreach ( $ image_meta ['sizes ' ] as $ size_name => $ size_meta ) {
518+ // Check if the output mime is enabled for this image size.
519+ if ( ! _wp_mime_type_available_for_image_size ( $ attachment_id , $ size_name , $ original_mime_type , $ mime_type ) ) {
520+ continue ;
521+ }
522+
515523 /*
516524 * Only checks "size name" so we don't override existing images even if the dimensions
517525 * don't match the currently defined size with the same name.
@@ -605,6 +613,25 @@ function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id, $mim
605613 return $ image_meta ;
606614}
607615
616+ /**
617+ * Check whether secondary mime output is available for a given size and mime type.
618+ *
619+ * @since 6.1.0
620+ *
621+ * @param int $attachment_id Attachment ID.
622+ * @param string $size_name Size name to check.
623+ * @return bool Whether the size is available for the given mime type.
624+ */
625+ function _wp_mime_type_available_for_image_size ( $ attachment_id , $ size_name , $ source_mime , $ destination_mime ) {
626+ if ( $ source_mime === $ destination_mime ) {
627+ return true ;
628+ }
629+ $ image_mime_transforms = wp_upload_image_mime_transforms ( $ attachment_id , $ size_name );
630+
631+ return isset ( $ image_mime_transforms [ $ source_mime ] [ $ destination_mime ] );
632+
633+ }
634+
608635/**
609636 * Low-level function to create full-size images in additional mime types.
610637 *
@@ -1336,28 +1363,59 @@ function _copy_image_file( $attachment_id ) {
13361363 * For example an `image/jpeg` should be converted into an `image/jpeg` and `image/webp`. The first type
13371364 * is considered the primary output type for this image.
13381365 *
1366+ * Called for each uploaded image to determine the list of mime types that should be converted into. Then,
1367+ * called again for each image size as they are generated to see if the image should be converted into the mime type
1368+ * for that size.
1369+ *
13391370 * @since 6.1.0
13401371 *
1341- * @param $attachment_id int The attachment ID.
1372+ * @param int $attachment_id The attachment ID.
1373+ * @param string $image_size The image size name. Optional.
13421374 * @return array An array of valid mime types, where the key is the source file mime type and the list of mime types to
13431375 * generate.
13441376 */
1345- function wp_upload_image_mime_transforms ( $ attachment_id ) {
1346- $ image_mime_transforms = array (
1347- 'image/jpeg ' => array ( 'image/jpeg ' , 'image/webp ' ),
1348- 'image/webp ' => array ( 'image/webp ' , 'image/jpeg ' ),
1377+ function wp_upload_image_mime_transforms ( $ attachment_id , $ image_size ) {
1378+
1379+ // For WordPress 6.1, only output WebP by default for core/core theme sizes.
1380+ $ default_core_sizes = array (
1381+ 'thumbnail ' ,
1382+ 'medium ' ,
1383+ 'medium_large ' ,
1384+ 'large ' ,
1385+ // 2x medium_large size.
1386+ '1536x1536 ' ,
1387+ // 2x large size.
1388+ '2048x2048 ' ,
1389+ // Twentyeleven theme.
1390+ 'large-feature ' ,
1391+ 'small-feature ' ,
1392+ // Twentyfourteen theme.
1393+ 'twentyfourteen-full-width ' ,
1394+ // Twentyseventeen theme.
1395+ 'twentyseventeen-featured-image ' ,
1396+ 'twentyseventeen-thumbnail-avatar ' ,
1397+ // Twentytwenty theme.
1398+ 'twentytwenty-fullscreen ' ,
13491399 );
1400+ $ image_mime_transforms = array ();
1401+ if ( empty ( $ image_size ) || in_array ( $ image_size , $ default_core_sizes ) ) {
1402+ $ image_mime_transforms = array (
1403+ 'image/jpeg ' => array ( 'image/webp ' ),
1404+ );
1405+ }
1406+
13501407
13511408 /**
1352- * Filter to the output mime types for a given input mime type.
1409+ * Filter to the output mime types for a given input mime type and image size .
13531410 *
13541411 * @since 6.1.0
13551412 *
1356- * @param array $image_mime_transforms A map with the valid mime transforms where the key is the source file mime type
1357- * and the value is one or more mime file types to generate.
1358- * @param int $attachment_id The ID of the attachment where the hook was dispatched.
1413+ * @param array $image_mime_transforms A map with the valid mime transforms where the key is the source file mime type
1414+ * and the value is one or more mime file types to generate.
1415+ * @param int $attachment_id The ID of the attachment where the hook was dispatched.
1416+ * @param string $image_size The image size name. Optional.
13591417 */
1360- return (array ) apply_filters ( 'wp_upload_image_mime_transforms ' , $ image_mime_transforms , $ attachment_id );
1418+ return (array ) apply_filters ( 'wp_upload_image_mime_transforms ' , $ image_mime_transforms , $ attachment_id, $ image_size );
13611419}
13621420
13631421/**
@@ -1371,7 +1429,7 @@ function wp_upload_image_mime_transforms( $attachment_id ) {
13711429 * @return array An array with two entries, the primary mime type and the list of additional mime types.
13721430 */
13731431function _wp_get_primary_and_additional_mime_types ( $ file , $ attachment_id ) {
1374- $ image_mime_transforms = wp_upload_image_mime_transforms ( $ attachment_id );
1432+ $ image_mime_transforms = wp_upload_image_mime_transforms ( $ attachment_id, false );
13751433 $ original_mime_type = wp_get_image_mime ( $ file );
13761434 $ output_mime_types = isset ( $ image_mime_transforms [ $ original_mime_type ] ) ? $ image_mime_transforms [ $ original_mime_type ] : array ( $ original_mime_type );
13771435
0 commit comments