wppaste
WordPress

wp_ajax_crop_image()

Since
4.3.0
Source
wp-admin/includes/ajax-actions.php:4009
Handles cropping an image via AJAX.

Compatibility

WordPress
since 4.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.

Performance profile

How much work a call to wp_ajax_crop_image() 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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
86–126

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

Plugin surface
4 hooks

Third-party callbacks on 'wp_create_file_in_uploads', 'wp_ajax_crop_image_pre_save', 'wp_ajax_cropped_attachment_metadata' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()one call below wp_ajax_crop_image()

Further down the call graph this can also reach 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 · 27 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_ajax_crop_image() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($attachment_id) && !is_wp_error() && $context === false86–88absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && current_user_can() && !is_wp_error() && $context === false89–91absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success()
empty($attachment_id) && $context === false89–91absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && !current_user_can() && !is_wp_error() && $context === false91–93absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && current_user_can() && $context === false92–94absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success()
empty($attachment_id) && is_wp_error() && $context === false93–95absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && !current_user_can() && $context === false94–96absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && current_user_can() && is_wp_error() && $context === false96–98absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && !current_user_can() && is_wp_error() && $context === false98–100absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success()
empty($attachment_id) && !is_wp_error()106–108absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && current_user_can() && !is_wp_error()109–111absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success()
empty($attachment_id)109–111absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success()
15 further outcomes, up to 126 instructions
!empty($attachment_id) && !current_user_can() && !is_wp_error()111–113absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success()
empty($attachment_id) && !is_wp_error() && $context !== false112–114absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && current_user_can()112–114absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success()
empty($attachment_id) && is_wp_error()113–115absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && !current_user_can()114–116absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && current_user_can() && !is_wp_error() && $context !== false115–117absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success()
empty($attachment_id) && $context !== false115–117absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && current_user_can() && is_wp_error()116–118absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && !current_user_can() && !is_wp_error() && $context !== false117–119absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && current_user_can() && $context !== false118–120absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && !current_user_can() && is_wp_error()118–120absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success()
empty($attachment_id) && is_wp_error() && $context !== false119–121absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && !current_user_can() && $context !== false120–122absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && current_user_can() && is_wp_error() && $context !== false122–124absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success()
!empty($attachment_id) && !current_user_can() && is_wp_error() && $context !== false124–126absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17986–1267
8.517986–1267
8.417986–12673 fewer instructions than PHP 8.3
8.318289–1297
8.218289–1297
8.118289–1297
7.418289–1297

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 · 5

5 hooks fire while wp_ajax_crop_image() runs, in this order:

  1. apply_filters( wp_create_file_in_uploads )filterline 4043 (+34 into the body)

    Filters the attachment file path after the custom header or background image is set.

  2. do_action( wp_ajax_crop_image_pre_save )actionline 4069 (+60 into the body)

    Fires before a cropped image is saved.

  3. apply_filters( wp_create_file_in_uploads )filterline 4072 (+63 into the body)

    Filters the attachment file path after the custom header or background image is set.

  4. apply_filters( wp_ajax_cropped_attachment_metadata )filterline 4089 (+80 into the body)

    Filters the cropped image attachment metadata.

  5. apply_filters( wp_ajax_cropped_attachment_id )filterline 4100 (+91 into the body)

    Filters the attachment ID for a cropped image.

Uses · 20

Show all 20

Source code

function wp_ajax_crop_image() {	$attachment_id = absint( $_POST['id'] ); 	check_ajax_referer( 'image_editor-' . $attachment_id, 'nonce' ); 	if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) {		wp_send_json_error();	} 	$context = str_replace( '_', '-', $_POST['context'] );	$data    = array_map( 'absint', $_POST['cropDetails'] );	$cropped = wp_crop_image( $attachment_id, $data['x1'], $data['y1'], $data['width'], $data['height'], $data['dst_width'], $data['dst_height'] ); 	if ( ! $cropped || is_wp_error( $cropped ) ) {		wp_send_json_error( array( 'message' => __( 'Image could not be processed.' ) ) );	} 	switch ( $context ) {		case 'site-icon':			require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php';			$wp_site_icon = new WP_Site_Icon(); 			// Skip creating a new attachment if the attachment is a Site Icon.			if ( get_post_meta( $attachment_id, '_wp_attachment_context', true ) === $context ) { 				// Delete the temporary cropped file, we don't need it.				wp_delete_file( $cropped ); 				// Additional sizes in wp_prepare_attachment_for_js().				add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) );				break;			} 			/** This filter is documented in wp-admin/includes/class-custom-image-header.php */			$cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. 			// Copy attachment properties.			$attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context ); 			// Update the attachment.			add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );			$attachment_id = $wp_site_icon->insert_attachment( $attachment, $cropped );			remove_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) ); 			// Additional sizes in wp_prepare_attachment_for_js().			add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) );			break; 		default:			/**			 * Fires before a cropped image is saved.			 *			 * Allows to add filters to modify the way a cropped image is saved.			 *			 * @since 4.3.0			 *			 * @param string $context       The Customizer control requesting the cropped image.			 * @param int    $attachment_id The attachment ID of the original image.			 * @param string $cropped       Path to the cropped image file.			 */			do_action( 'wp_ajax_crop_image_pre_save', $context, $attachment_id, $cropped ); 			/** This filter is documented in wp-admin/includes/class-custom-image-header.php */			$cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. 			// Copy attachment properties.			$attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context ); 			$attachment_id = wp_insert_attachment( $attachment, $cropped );			$metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped ); 			/**			 * Filters the cropped image attachment metadata.			 *			 * @since 4.3.0			 *			 * @see wp_generate_attachment_metadata()			 *			 * @param array $metadata Attachment metadata.			 */

Changelog

Introduced in 4.3.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.8.8 tag, from src/wp-admin/includes/ajax-actions.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.