@@ -492,11 +492,11 @@ function _wp_get_sources_from_meta( $meta ) {
492
492
* @since 6.1.0 The $mime_type parameter was added.
493
493
* @access private
494
494
*
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.
500
500
* @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
501
501
*/
502
502
function _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
505
505
return array ();
506
506
}
507
507
508
+ $ original_mime_type = wp_get_image_mime ( $ file )
509
+
508
510
if ( ! $ mime_type ) {
509
- $ mime_type = wp_get_image_mime ( $ file ) ;
511
+ $ mime_type = $ original_mime_type ;
510
512
}
511
513
514
+
512
515
// Check if any of the new sizes already exist.
513
516
if ( isset ( $ image_meta ['sizes ' ] ) && is_array ( $ image_meta ['sizes ' ] ) ) {
514
517
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
+
515
523
/*
516
524
* Only checks "size name" so we don't override existing images even if the dimensions
517
525
* 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
605
613
return $ image_meta ;
606
614
}
607
615
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
+
608
635
/**
609
636
* Low-level function to create full-size images in additional mime types.
610
637
*
@@ -1336,28 +1363,59 @@ function _copy_image_file( $attachment_id ) {
1336
1363
* For example an `image/jpeg` should be converted into an `image/jpeg` and `image/webp`. The first type
1337
1364
* is considered the primary output type for this image.
1338
1365
*
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
+ *
1339
1370
* @since 6.1.0
1340
1371
*
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.
1342
1374
* @return array An array of valid mime types, where the key is the source file mime type and the list of mime types to
1343
1375
* generate.
1344
1376
*/
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 themes.
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 ' ,
1349
1399
);
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
+
1350
1407
1351
1408
/**
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 .
1353
1410
*
1354
1411
* @since 6.1.0
1355
1412
*
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.
1359
1417
*/
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 );
1361
1419
}
1362
1420
1363
1421
/**
@@ -1371,7 +1429,7 @@ function wp_upload_image_mime_transforms( $attachment_id ) {
1371
1429
* @return array An array with two entries, the primary mime type and the list of additional mime types.
1372
1430
*/
1373
1431
function _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 );
1375
1433
$ original_mime_type = wp_get_image_mime ( $ file );
1376
1434
$ output_mime_types = isset ( $ image_mime_transforms [ $ original_mime_type ] ) ? $ image_mime_transforms [ $ original_mime_type ] : array ( $ original_mime_type );
1377
1435
0 commit comments