Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
_wp_make_subsizes() WordPress Function
The _wp_make_subsizes() function is used to create new image sizes for a WordPress site. This is useful for responsive design, where you might want to have different image sizes for different screen sizes. The function takes three parameters: The first parameter is the name of the image size. The second parameter is the width of the image in pixels. The third parameter is the height of the image in pixels. The function will return an array of image sizes, which can then be used in your theme.
_wp_make_subsizes( array $new_sizes, string $file, array $image_meta, int $attachment_id ) #
Low-level function to create image sub-sizes.
Description
Updates the image meta after each sub-size is created. Errors are stored in the returned image metadata array.
Parameters
- $new_sizes
(array)(Required)Array defining what sizes to create.
- $file
(string)(Required)Full path to the image file.
- $image_meta
(array)(Required)The attachment meta data array.
- $attachment_id
(int)(Required)Attachment ID to process.
Return
(array) The attachment meta data with updated sizes
array. Includes an array of errors encountered while resizing.
Source
File: wp-admin/includes/image.php
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | function _wp_make_subsizes( $new_sizes , $file , $image_meta , $attachment_id ) { if ( empty ( $image_meta ) || ! is_array ( $image_meta ) ) { // Not an image attachment. return array (); } // Check if any of the new sizes already exist. if ( isset( $image_meta [ 'sizes' ] ) && is_array ( $image_meta [ 'sizes' ] ) ) { foreach ( $image_meta [ 'sizes' ] as $size_name => $size_meta ) { /* * Only checks "size name" so we don't override existing images even if the dimensions * don't match the currently defined size with the same name. * To change the behavior, unset changed/mismatched sizes in the `sizes` array in image meta. */ if ( array_key_exists ( $size_name , $new_sizes ) ) { unset( $new_sizes [ $size_name ] ); } } } else { $image_meta [ 'sizes' ] = array (); } if ( empty ( $new_sizes ) ) { // Nothing to do... return $image_meta ; } /* * Sort the image sub-sizes in order of priority when creating them. * This ensures there is an appropriate sub-size the user can access immediately * even when there was an error and not all sub-sizes were created. */ $priority = array ( 'medium' => null, 'large' => null, 'thumbnail' => null, 'medium_large' => null, ); $new_sizes = array_filter ( array_merge ( $priority , $new_sizes ) ); $editor = wp_get_image_editor( $file ); if ( is_wp_error( $editor ) ) { // The image cannot be edited. return $image_meta ; } // If stored EXIF data exists, rotate the source image before creating sub-sizes. if ( ! empty ( $image_meta [ 'image_meta' ] ) ) { $rotated = $editor ->maybe_exif_rotate(); if ( is_wp_error( $rotated ) ) { // TODO: Log errors. } } if ( method_exists( $editor , 'make_subsize' ) ) { foreach ( $new_sizes as $new_size_name => $new_size_data ) { $new_size_meta = $editor ->make_subsize( $new_size_data ); if ( is_wp_error( $new_size_meta ) ) { // TODO: Log errors. } else { // Save the size meta value. $image_meta [ 'sizes' ][ $new_size_name ] = $new_size_meta ; wp_update_attachment_metadata( $attachment_id , $image_meta ); } } } else { // Fall back to `$editor->multi_resize()`. $created_sizes = $editor ->multi_resize( $new_sizes ); if ( ! empty ( $created_sizes ) ) { $image_meta [ 'sizes' ] = array_merge ( $image_meta [ 'sizes' ], $created_sizes ); wp_update_attachment_metadata( $attachment_id , $image_meta ); } } return $image_meta ; } |
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
5.3.0 | Introduced. |