image_edit_apply_changes( WP_Image_Editor $image, array $changes ): WP_Image_Editor
- Since
- 2.9.0
- Source
wp-admin/includes/image-edit.php:635
Compatibility
- WordPress
- since 2.9.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
$imageWP_Image_Editor- WP_Image_Editor instance.
$changesarray- Array of change operations.
Return value
WP_Image_Editor- WP_Image_Editor instance with changes applied.
Performance profile
How much work a call to image_edit_apply_changes() 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
- Light
- Scaling
- Scales with input
- Instructions
- 9–56
- Plugin surface
- 2 hooks
- Called by
- 2
Touches nothing outside its own arguments.
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 260.
Third-party callbacks on 'wp_image_editor_before_change', 'image_edit_before_change' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach query, option, cache, serialize 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost image_edit_apply_changes() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_gd_image() && !is_array($changes) | 9 | is_gd_image() |
!is_gd_image() && is_array($changes) && !($image instanceof) | 22–43 | is_gd_image(), is_gd_image() |
is_gd_image() && !is_array($changes) | 22 | is_gd_image(), __(), sprintf(), _deprecated_argument() |
!is_gd_image() && is_array($changes) && $image instanceof | 25–37 | is_gd_image(), apply_filters() |
is_gd_image() && is_array($changes) && !($image instanceof) | 35–56 | is_gd_image(), __(), sprintf(), _deprecated_argument(), is_gd_image() |
is_gd_image() && is_array($changes) && $image instanceof | 38–50 | is_gd_image(), __(), sprintf(), _deprecated_argument(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 260 | 9–56 | 29 | |
| 8.5 | 260 | 9–56 | 29 | |
| 8.4 | 260 | 9–56 | 29 | |
| 8.3 | 260 | 9–56 | 29 | |
| 8.2 | 260 | 9–56 | 29 | 2 more instructions than PHP 8.1 |
| 8.1 | 258 | 9–56 | 29 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 262 | 9–56 | 29 |
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.
Hooks and filters fired · 2
2 hooks fire while image_edit_apply_changes() runs, in this order:
- apply_filters( wp_image_editor_before_change )filterline 704 (+69 into the body)
Filters the WP_Image_Editor instance before applying changes to the image.
- do_action( image_edit_before_change )filter_deprecatedline 716 (+81 into the body)
Filters the GD image resource before applying changes to the image.
Uses · 9
- is_gd_image()Determines whether the value is an acceptable type for GD image functions.
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- apply_filters_deprecated()Fires functions attached to a deprecated filter hook.
- _rotate_image_resource()Returns an image resource. Internal use only.
- _flip_image_resource()Flips an image resource. Internal use only.
- _image_get_preview_ratio()Image preview ratio. Internal use only.
- _crop_image_resource()Crops an image resource. Internal use only.
Used by · 2
- stream_preview_image()Streams image in post to browser, along with enqueued changes in `$_REQUEST['history']`.
- wp_save_image()Saves image to post, along with enqueued changes in `$_REQUEST['history']`.
Source code
function image_edit_apply_changes( $image, $changes ) { if ( is_gd_image( $image ) ) { /* translators: 1: $image, 2: WP_Image_Editor */ _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) ); } if ( ! is_array( $changes ) ) { return $image; } // Expand change operations. foreach ( $changes as $key => $obj ) { if ( isset( $obj->r ) ) { $obj->type = 'rotate'; $obj->angle = $obj->r; unset( $obj->r ); } elseif ( isset( $obj->f ) ) { $obj->type = 'flip'; $obj->axis = $obj->f; unset( $obj->f ); } elseif ( isset( $obj->c ) ) { $obj->type = 'crop'; $obj->sel = $obj->c; unset( $obj->c ); } $changes[ $key ] = $obj; } // Combine operations. if ( count( $changes ) > 1 ) { $filtered = array( $changes[0] ); for ( $i = 0, $j = 1, $c = count( $changes ); $j < $c; $j++ ) { $combined = false; if ( $filtered[ $i ]->type === $changes[ $j ]->type ) { switch ( $filtered[ $i ]->type ) { case 'rotate': $filtered[ $i ]->angle += $changes[ $j ]->angle; $combined = true; break; case 'flip': $filtered[ $i ]->axis ^= $changes[ $j ]->axis; $combined = true; break; } } if ( ! $combined ) { $filtered[ ++$i ] = $changes[ $j ]; } } $changes = $filtered; unset( $filtered ); } // Image resource before applying the changes. if ( $image instanceof WP_Image_Editor ) { /** * Filters the WP_Image_Editor instance before applying changes to the image. * * @since 3.5.0 * * @param WP_Image_Editor $image WP_Image_Editor instance. * @param array $changes Array of change operations. */ $image = apply_filters( 'wp_image_editor_before_change', $image, $changes ); } elseif ( is_gd_image( $image ) ) { /** * Filters the GD image resource before applying changes to the image. * * @since 2.9.0 * @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead. * * @param resource|GdImage $image GD image resource or GdImage instance. * @param array $changes Array of change operations.Changelog
Introduced in 2.9.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.9.7 tag, from
src/wp-admin/includes/image-edit.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.