wppaste
WordPress

image_edit_apply_changes( WP_Image_Editor $image, array $changes ): WP_Image_Editor

Since
2.9.0
Source
wp-admin/includes/image-edit.php:626
Performs group of changes on Editor specified.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
9–56

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 260.

Plugin surface
2 hooks

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.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
!is_gd_image() && !is_array($changes)9is_gd_image()
!is_gd_image() && is_array($changes) && !($image instanceof)22–43is_gd_image(), is_gd_image()
is_gd_image() && !is_array($changes)22is_gd_image(), __(), sprintf(), _deprecated_argument()
!is_gd_image() && is_array($changes) && $image instanceof25–37is_gd_image(), apply_filters()
is_gd_image() && is_array($changes) && !($image instanceof)35–56is_gd_image(), __(), sprintf(), _deprecated_argument(), is_gd_image()
is_gd_image() && is_array($changes) && $image instanceof38–50is_gd_image(), __(), sprintf(), _deprecated_argument(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2609–5629
8.52609–5629
8.42609–5629
8.32609–5629
8.22609–56292 more instructions than PHP 8.1
8.12589–56294 fewer instructions than PHP 7.4
7.42629–5629

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:

  1. apply_filters( wp_image_editor_before_change )filterline 695 (+69 into the body)

    Filters the WP_Image_Editor instance before applying changes to the image.

  2. do_action( image_edit_before_change )filter_deprecatedline 707 (+81 into the body)

    Filters the GD image resource before applying changes to the image.

Uses · 9

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.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.