_wp_make_subsizes( array $new_sizes, string $file, array $image_meta, int $attachment_id ): array
- Since
- 5.3.0
- Source
wp-admin/includes/image.php:430
Description
Updates the image meta after each sub-size is created.
Errors are stored in the returned image metadata array.
Compatibility
- WordPress
- since 5.3.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$new_sizesarray- Array defining what sizes to create.
$filestring- Full path to the image file.
$image_metaarray- The attachment meta data array.
$attachment_idint- Attachment ID to process.
Return value
array- The attachment meta data with updated
sizesarray. Includes an array of errors encountered while resizing.
Performance profile
How much work a call to _wp_make_subsizes() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 7–67
- Plugin surface
- None
- Called by
- 3
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 98.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below _wp_make_subsizes() - querycontent query
get_post()one call below _wp_make_subsizes()
Further down the call graph this can also reach cache, serialize, option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_make_subsizes() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–21 | none |
!empty($image_meta) && is_array($image_meta) && !empty($new_sizes) && is_wp_error() | 31–37 | array_merge(), array_filter(), wp_get_image_editor(), is_wp_error() |
is_array($image_meta) && !empty($new_sizes) && !is_wp_error() && method_exists() | 40–47 | array_merge(), array_filter(), wp_get_image_editor(), is_wp_error(), method_exists() |
is_array($image_meta) && !empty($new_sizes) && !is_wp_error() && !method_exists() && empty($created_sizes) | 44–50 | array_merge(), array_filter(), wp_get_image_editor(), is_wp_error(), method_exists(), ->multi_resize() |
!empty($image_meta) && is_array($image_meta) && !empty($new_sizes) && !is_wp_error() && method_exists() | 46–53 | array_merge(), array_filter(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), is_wp_error(), method_exists() |
!empty($image_meta) && is_array($image_meta) && !empty($new_sizes) && !is_wp_error() && !method_exists() && empty($created_sizes) | 50–56 | array_merge(), array_filter(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), is_wp_error(), method_exists(), ->multi_resize() |
is_array($image_meta) && !empty($new_sizes) && !is_wp_error() && !method_exists() && !empty($created_sizes) | 55–61 | array_merge(), array_filter(), wp_get_image_editor(), is_wp_error(), method_exists(), ->multi_resize(), array_merge(), wp_update_attachment_metadata() |
!empty($image_meta) && is_array($image_meta) && !empty($new_sizes) && !is_wp_error() && !method_exists() && !empty($created_sizes) | 61–67 | array_merge(), array_filter(), wp_get_image_editor(), is_wp_error(), ->maybe_exif_rotate(), is_wp_error(), method_exists(), ->multi_resize(), array_merge(), wp_update_attachment_metadata() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 98 instructions, 7–67 executed per call, 15 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- wp_get_image_editor()Returns a WP_Image_Editor instance and loads file into it.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_update_attachment_metadata()Updates metadata for an attachment.
Used by · 3
- wp_create_image_subsizes()Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- wp_update_image_subsizes()If any of the currently registered image sub-sizes are missing, create them and update the image meta data.
Source code
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;Changelog
Introduced in 5.3.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-admin/includes/image.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.